使用getErrorStream从getRuntime()。exec执行的ksh脚本中捕获错误消息?

时间:2014-06-12 10:48:20

标签: java unix ksh

我需要从java执行ksh脚本,我想在其中退出并显示错误消息。

退出1 - 退出并显示错误代码1

但是我应该在脚本中做什么才能用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);
  }
}

1 个答案:

答案 0 :(得分:0)

您需要在进程运行时使用stdout和stderr ,而不是在完成后使用它。

请注意,您必须在单独的线程中执行此操作,否则您的进程将面临阻止的风险。有关详细信息,请参阅我的回答here