0

How to get error message when execution binary file with Runtime?

My code is:

Runtime localRuntime = Runtime.getRuntime();
String strExec = "myBinary -s -a myconfigbinary.conf";
try {
    localRuntime.exec(strExec);
    System.out.println("Success execute."); 
} catch (IOException e) {
    System.out.println(e.getMessage().toString());      
}

My code is above, I only get the exception error when the file does not exist. but if I run using the console on the computer when it did not work because of the error in the configuration file or whatever is causing the binary is not running, but I still get message Success execute..

My question is i want to get error message like when i get an error on the console on the computer. How to use the correct exception in this case?

Thanks.

2 Answers 2

1

You can check the error stream like

Process process=localRuntime.exec(strExec);
process.waitFor();
InputStream errorStream = process.getErrorStream();
Sign up to request clarification or add additional context in comments.

Comments

1

You should also capture the ErrorStream of the process in java, as

try {
   Process proc = localRuntime.exec(strExec);
   InputStream stderr = proc.getErrorStream();
   InputStreamReader is = new InputStreamReader(stderr);
   BufferedReader br = new BufferedReader(is);
   String line = null;
   while ( (line = br.readLine()) != null)
          System.out.println(line);
   int exitVal = proc.waitFor();
   System.out.println("Process exitValue: " + exitVal);
} catch (Throwable t) {
    t.printStackTrace();
}

If the error from the exec'd process goes to stdout, you might need to do the above for stdout as well.

1 Comment

get stack and no load view in line proc.waitFor();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.