6

I'm trying to execute a shell command from a java application, on the GNU/Linux platform. The problem is that the script, that calls another java application, never ends, although 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 with: ProcessBuilder(); and Runtime.getRuntime().exec(cmd);

Looks like it waits for something to finish. Any ideas?

Thanks, Laurențiu

Laurențiu Dascălu
  • 2,039
  • 3
  • 22
  • 23
  • Are you saying that the second Java program is expected to finish quickly but the shell invoking it still hangs around? Have you confirmed the second Java process is actually finished (cheking process listing)? – Paul Jowett Jun 17 '10 at 14:21
  • Yes I checked the processes list and the process is still there. I think that dsmith answered my question. – Laurențiu Dascălu Jun 18 '10 at 15:50
  • Looks like the Process class from Java is terrible slow. I started the java application from the shell with production (non-verbose) parameters and it finished after a while. – Laurențiu Dascălu Jun 17 '10 at 14:26

1 Answers1

15

Are you processing the standard input and standard output? From the javadocs:

Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.

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();
Hugo Zaragoza
  • 574
  • 8
  • 25
dsmith
  • 1,978
  • 10
  • 16