R
Reid Madsen
I'm using Runtime.exec() to run a command via xargs so that I can pass
a long list of arguments (too long to put on the command line). 99% of
the time this works perfectly. In the 1% case that fails, the xargs process
hangs reading from its input stream. It acts as if it has not detected that
we have closed its input stream in the java application.
I'm running the JDK 1.4.1_03 version on Solaris 2.9 with 24 CPU's.
Here are the basic elements of this implementation:
1) I have created a subprocess using:
Process xargs = Runtime.exec("/bin/xargs cleartool lsview -long");
BufferedReader stdOut =
new BufferedReader(new InputStreamReader(xargs.getInputStream()));
BufferedReader stdErr =
new BufferedReader(new InputStreamReader(xargs.getErrorStream()));
PrintStream stdIn =
new PrintStream(new BufferedOutputStream(xargs.getOutputStream()), true);
2) I have then created two threads, one to dispose of stderr from xargs,
and another to feed input into xargs:
new Thread() {
public void run() {
// Discard all output on stderr
while (stdErr.readln() != null);
}
}.start();
// The list of arguments to pass to the process via /bin/xargs
String[] args= <Array of strings>;
new Thread() {
public void run() {
// Feed arguments to process stdin
for (int i=0; i<args.length; ++i) {
stdIn.println(args);
// close process stdin to signal end of input
// process should then process arguments and exit
stdIn.flush();
stdIn.close();
}
}.start();
3) Then I process the output from process stdout directly:
String input;
while ((input = stdOut.readln()) != null) {
// process input
}
4) Finally I wait for the process to exit:
int exitStatus = xargs.waitFor();
This seems to be the correct way of doing this, and it works
most of the time. Has anyone experienced this problem before?
If so, what is the solution? Or, can you give me some ideas of where to look?
Thank you in advance for any help you may offer.
Reid Madsen
i2 Technologies
a long list of arguments (too long to put on the command line). 99% of
the time this works perfectly. In the 1% case that fails, the xargs process
hangs reading from its input stream. It acts as if it has not detected that
we have closed its input stream in the java application.
I'm running the JDK 1.4.1_03 version on Solaris 2.9 with 24 CPU's.
Here are the basic elements of this implementation:
1) I have created a subprocess using:
Process xargs = Runtime.exec("/bin/xargs cleartool lsview -long");
BufferedReader stdOut =
new BufferedReader(new InputStreamReader(xargs.getInputStream()));
BufferedReader stdErr =
new BufferedReader(new InputStreamReader(xargs.getErrorStream()));
PrintStream stdIn =
new PrintStream(new BufferedOutputStream(xargs.getOutputStream()), true);
2) I have then created two threads, one to dispose of stderr from xargs,
and another to feed input into xargs:
new Thread() {
public void run() {
// Discard all output on stderr
while (stdErr.readln() != null);
}
}.start();
// The list of arguments to pass to the process via /bin/xargs
String[] args= <Array of strings>;
new Thread() {
public void run() {
// Feed arguments to process stdin
for (int i=0; i<args.length; ++i) {
stdIn.println(args);
// close process stdin to signal end of input
// process should then process arguments and exit
stdIn.flush();
stdIn.close();
}
}.start();
3) Then I process the output from process stdout directly:
String input;
while ((input = stdOut.readln()) != null) {
// process input
}
4) Finally I wait for the process to exit:
int exitStatus = xargs.waitFor();
This seems to be the correct way of doing this, and it works
most of the time. Has anyone experienced this problem before?
If so, what is the solution? Or, can you give me some ideas of where to look?
Thank you in advance for any help you may offer.
Reid Madsen
i2 Technologies