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 :)

Community
  • 1
  • 1
bohr
  • 631
  • 2
  • 9
  • 29
  • Not working means? No output or are you getting any exception? – TheCodingFrog Jun 27 '15 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 – bohr Jun 27 '15 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()`. – Hovercraft Full Of Eels Jun 27 '15 at 13:59
  • I am afraid I don't really get what you mean. could you please give me an example? cheers – bohr Jun 27 '15 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. – Hovercraft Full Of Eels Jun 27 '15 at 14:27
  • Here's an example of a [search of StackOverflow](http://stackoverflow.com/search?q=%5Bjava%5D+run+a+process+reading+streams+thread) on similar questions. – Hovercraft Full Of Eels Jun 27 '15 at 14:29

1 Answers1

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.

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183