0

I need to execute ksh script from java, where I want to exit with error and provide error message.

exit 1 - exits with error code 1

But what should I do in script in order to catch the error message with getErrorStream?

 proc =    Runtime.getRuntime().exec(SCRIPT_PATH);

 int exitV = proc.waitFor();
if(exitV !=0){
  InputStream iputStream= proc.getErrorStream();
  BufferedReader iput = new BufferedReader(new InputStreamReader(iputStream));
  while ((line = iput.readLine()) != null){
   msg.append(line);
  }
}
yuris
  • 1,109
  • 4
  • 19
  • 33

1 Answers1

0

You need to consume both stdout and stderr whilst the process is running, rather than once its completed.

Note that you have to do this in separate threads, otherwise your process runs the risk of blocking. See my answer here for more details.

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440