Related, and simpler, question -
Q: When I execute the following code, the shell process executed here hangs. Also, the file /tmp/secLevel is never created (and never modified if it already exists).
Runtime.getRuntime().exec("/bin/ksh -c /usr/local/bin/getSecLevel > /tmp/secLevel");
Here are the contents of /usr/local/bin/getSecLevel:
#!/bin/ksh
level=`replace me later`
echo $level
exit
Process proc = Runtime.getRuntime().exec("command");
InputStream in = proc.getInputStream();
proc.waitFor();
byte[] result = new byte[in.available()];
in.read(result);
in.close();
System.out.println(new String(result));
it works only if no errors occurs.
To catch Errors use getErrorStream() method.
good luck
It also works only if the OS inter-process (pipe) buffer doesn't fill up before the command exits. Typically this is a fairly small buffer, so the chances are high that it will.
Better is to use something approaching this technique, but to read the stream using a separate thread. You can re-use your thread to separately read the error stream too.