bohr
bohr

Reputation: 661

Running .sh from Java code

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

Upvotes: 0

Views: 289

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122414

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.

Upvotes: 1

Related Questions