I am trying to execute a third-party command-line application from Java. The application is called CWB. I'm using the following code:
process = Runtime.getRuntime().exec(script.sh);
LinkedList<String> stringList = new LinkedList<String>();
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
stringList.add(line);
}
int exitVal = 0;
exitVal = process.waitFor();
if (exitVal == 0) {
// Success
return stringList;
} else {
// Failure
return null;
}
The commands are put in a file script.sh
(I'm in Linux). The script contains the following two commands:
# Run CWB application from Terminal
./cwb-nc-ccs-x86-linux.bin
# Execute a CWB-specific command
load file_name.ccs
The behavior is different when I execute CWB from the terminal and when I execute it in Java through Runtime.getRuntime().exec()
. When I execute CWB from Terminal, I obtain this output:
But, when I execute from Java through Runtime.getRuntime().exec()
, I obtain this output in the console:
The Concurrency Workbench of the New Century
(Version 1.2 --- June, 2000)
That is, the shell in Java stops execution after the command ./cwb-nc-ccs-x86-linux.bin
. I think the reason is that, when I start the CWB process, it's like as if I enter in a new "environment". It can be noticed because the cursor line where I type the commands starts with cwb-nc>
, as we can see in the picture above. Thus, being in a new "environment" and not in the shell, Java doesn't know how to pass commands to this environment. Am I right? How can I solve this problem and execute the other command in the script (load file_name.ccs
)?
If you had tried to run your script outside of Java, you'd see that it doesn't work there either. You should focus on writing a script that works first, and only try invoking it from Java when it does.
The problem with the script is detailed here: Pass commands as input to another command (su, ssh, sh, etc).
However, since you're running from Java, you could and probably should implement this purely in Java without a shell script:
process = Runtime.getRuntime().exec(new String[] { "./cwb-nc-ccs-x86-linux.bin" });
PrintStream writer = new PrintStream(process.getOutputStream());
writer.println("load file_name.ccs");
writer.close();
...