Reputation: 954
I am building a javaFx application I am running a command using java process builder class.
ProcessBuilder builder = new ProcessBuilder(runCommand);
builder.redirectErrorStream(true);
Process process = builder.start();
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
I am trying to get the output from the process I have tried two different ways but I am not able to get it.
InputStream stdout = process.getInputStream();
InputStream stderr = process.getErrorStream ();
BufferedReader reader = new BufferedReader (new InputStreamReader(stdout));
String line1=null;
while ((line1 = reader.readLine()) != null) {
System.out.println(line1);
}
BufferedReader errorred = new BufferedReader (new InputStreamReader(stderr));
while ((line1 = errorred.readLine()) != null) {
System.out.println(line1);
}
I tried also this:
ProcessBuilder builder = new ProcessBuilder(runCommand);
//String outputLogs = new VerboseProcess(new ProcessBuilder(""));
builder.redirectErrorStream(true);
builder.redirectOutput(Redirect.INHERIT);
builder.redirectError(Redirect.INHERIT);
builder.redirectInput(Redirect.INHERIT);
Process process = builder.start();
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
Upvotes: 1
Views: 2649
Reputation: 22993
I believe the error is somewhere else. Following snippet is based on your posted code
ProcessBuilder builder = new ProcessBuilder("java.exe", "-version");
builder.redirectErrorStream(true);
Process process = builder.start();
process.waitFor();
int exitValue = process.exitValue();
System.out.println("exitValue = " + exitValue);
InputStream stdout = process.getInputStream();
InputStream stderr = process.getErrorStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
String line1 = null;
while ((line1 = reader.readLine()) != null) {
System.out.println(line1);
}
BufferedReader errorred = new BufferedReader(new InputStreamReader(stderr));
while ((line1 = errorred.readLine()) != null) {
System.out.println(line1);
}
and produces the following output on the console
exitValue = 0
java version "1.8.0_74"
Java(TM) SE Runtime Environment (build 1.8.0_74-b02)
Java HotSpot(TM) 64-Bit Server VM (build 25.74-b02, mixed mode)
If the command specified in new ProcessBuilder(command...)
does not exists or is not in the PATH an IOException
is thrown
Cannot run program "...": CreateProcess error=2, The system \
cannot find the file specified
If you don't get any output. Following could be the reason:
runcommand
start the process in a background process, e.g. on Linux/OSX command &
or on Windows start command.exe
Upvotes: 1