0

I tried to run .sh file from my java code. I've seen a similar questions (How to run Unix shell script from Java code?) and tried the answers that was written there. However, I expect the process exit value will be 0 (terminate normally), while in fact I got 127. Here is my code:

public int performATRs() throws IOException{

    String[] command = {"sh", "/ThematicAnalysis/flexiTerm/FlexiTerm.sh"};
    Process process = Runtime.getRuntime().exec(command);
    InputStream inputStream = process.getInputStream();
    InputStreamReader streamReader = new InputStreamReader(inputStream);
    BufferedReader bufReader = new BufferedReader(streamReader);
    try{
        process.waitFor();
        System.out.println("Waiting...");
        System.out.println("Returned Value :" + process.exitValue());
    }catch(InterruptedException e){
        System.out.println(e.getMessage());
    }
    while(bufReader.ready())
        System.out.println(bufReader.readLine());
    return process.exitValue();
} 

I tried to run FlexiTerm.sh from my java code. It works when I run it from the terminal though. Thank you very much for your help :)

6
  • Not working means? No output or are you getting any exception? Commented Jun 27, 2015 at 12:37
  • thank you for your response. When I tried to return the process.exitValue(), I got 1 instead of 0 (normal termination) @my-thoughts Commented Jun 27, 2015 at 12:46
  • You're using only a single thread for this code and then blocking this single thread by calling waitFor() at a point before your process is able to communicate any information back to you. Consider 1) getting a BufferedReader for the error stream as well as the input stream, start reading from the streams in threads of their own and before calling waitFor(). Commented Jun 27, 2015 at 13:59
  • I am afraid I don't really get what you mean. could you please give me an example? cheers Commented Jun 27, 2015 at 14:23
  • What you don't understand how to create a new thread? Rather than ask a major undertaking by any of us, please be very specific in your question. Surely you've looked up examples of this sort of thing and have seen threads being used to do this, right? Again, please clarify just what confuses you about this. Commented Jun 27, 2015 at 14:27

1 Answer 1

1

127 means it wasn't able to execute /ThematicAnalysis/flexiTerm/FlexiTerm.sh, you should double check that the path to that script is correct. I suspect it isn't, given that a Unix-style system wouldn't normally have a top-level directory named "ThematicAnalysis". If you're looking for that script at a location relative to your Java program's working directory then you should remove the leading forward slash.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.