Execute shell commands from Java
•
Java
I am trying to execute shell commands from Java applications on GNU / Linux platform The problem is that the script that calls another Java application never ends, even though it runs successfully from bash I tried to debug it:
(gdb) bt #0 0xb773d422 in __kernel_vsyscall () #1 0xb7709b5d in pthread_join (threadid=3063909232,thread_return=0xbf9cb678) at pthread_join.c:89 #2 0x0804dd78 in ContinueInNewThread () #3 0x080497f6 in main ()
I tried: processbuilder(); And runtime getRuntime(). exec(cmd);
It looks like it's waiting to finish something Any ideas?
Thank you, Lauren ţ iu
Solution
Are you working with standard input and standard output? From JavaDocs:
Process cmdProc = Runtime.getRuntime().exec(command);
BufferedReader stdoutReader = new BufferedReader(
new InputStreamReader(cmdProc.getInputStream()));
String line;
while ((line = stdoutReader.readLine()) != null) {
// process procs standard output here
}
BufferedReader stderrReader = new BufferedReader(
new InputStreamReader(cmdProc.getErrorStream()));
while ((line = stderrReader.readLine()) != null) {
// process procs standard error here
}
int retValue = cmdProc.exitValue();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
