在程序中调用外部程序,用process返回命令执行结果以及抓取错误信息的方法
2011-04-26 17:34
1201 查看
在Java代码里调用外部程序时,比如一个ksh等,可以用如下方法:
The
所以在调用外部程序时,使用process将启动一个新进程,用于执行外部程序。使用waitfor()等待外部程序执行完毕并返回值。
String command = String.format(CHECK_GRAPH_IS_RUNNING_FORMAT, recoveryFile.getAbsolutePath());
Process process = execCommand(command);
int isRunningExitCode = process.waitFor();
return (SUCCESS_EXIT_VALUE == isRunningExitCode);
protected Process execCommand(final String command) throws IOException {
return Runtime.getRuntime().exec(command, getEnvp(), getWorkingFolder());
}
process.waitFor(); causes the current thread to wait, if necessary, until the process represented by this
the exit value of the process. By convention,
返回的这个参数isRunningExitCode 就是command运行的返回值。
此外,当运行外部程序出现错误时,可以很方便的抓取返回信息并进一步加以分析。
private Process process;
process = launchTaskProcess();
processOutStreamLog = process == null ? EMPTY : NEW_LINE + "Task process out stream >>>> " + NEW_LINE
+ readInputStream(process.getInputStream());
processErrorStreamLog = process == null ? EMPTY : NEW_LINE + "Task process error stream >>>> "
+ NEW_LINE + readInputStream(process.getErrorStream());
这里的process.getInputStream() 和 process.getErrorStream() 就可以取得执行的process的outputmessage.
The
ProcessBuilder.start()and
Runtime.execmethods create a native process and return an instance of a subclass of
Processthat can be used to control the process and obtain information about it.
所以在调用外部程序时,使用process将启动一个新进程,用于执行外部程序。使用waitfor()等待外部程序执行完毕并返回值。
String command = String.format(CHECK_GRAPH_IS_RUNNING_FORMAT, recoveryFile.getAbsolutePath());
Process process = execCommand(command);
int isRunningExitCode = process.waitFor();
return (SUCCESS_EXIT_VALUE == isRunningExitCode);
protected Process execCommand(final String command) throws IOException {
return Runtime.getRuntime().exec(command, getEnvp(), getWorkingFolder());
}
process.waitFor(); causes the current thread to wait, if necessary, until the process represented by this
Processobject has terminated. This method returns immediately if the subprocess has already terminated. If the subprocess has not yet terminated, the calling thread will be blocked until the subprocess exits.
the exit value of the process. By convention,
0indicates normal termination.
返回的这个参数isRunningExitCode 就是command运行的返回值。
此外,当运行外部程序出现错误时,可以很方便的抓取返回信息并进一步加以分析。
private Process process;
process = launchTaskProcess();
processOutStreamLog = process == null ? EMPTY : NEW_LINE + "Task process out stream >>>> " + NEW_LINE
+ readInputStream(process.getInputStream());
processErrorStreamLog = process == null ? EMPTY : NEW_LINE + "Task process error stream >>>> "
+ NEW_LINE + readInputStream(process.getErrorStream());
这里的process.getInputStream() 和 process.getErrorStream() 就可以取得执行的process的outputmessage.
相关文章推荐
- C#中利用process类调用外部程序以及执行dos命令
- Process 执行Linux命令/调用外部程序
- JAVA学习22_在Java中执行脚本命令以及调用外部程序说明
- C#中利用process类调用外部程序以及执行dos命令 - ASP.NET
- Linux C 程序执行 shell 命令并获取返回结果的方法
- java 执行命令、调用bat文件、调用外部程序的两种方法
- 在Java中执行脚本命令以及调用外部程序说明
- 用NSTask执行外部命令并获取输出结果的方法
- winform c#调用外部EXE程序,并等待外部EXE程序执行完毕才执行下面的代码的方法。
- C#中利用process类调用外部程序以及执行dos命令
- C#中利用Process类调用外部程序以及执行dos命令(转)
- 使用ProcessBuilder调用外部命令,并返回大量结果
- c# 使用Process调用外部程序时等待该进程结束后再执行住进程
- 用NSTask执行外部命令并获取输出结果的方法
- javac错误:javac不是内部或外部命令 也不是可运行的程序 解决方法
- C#中利用process类调用外部程序以及执行dos命令
- 在执行yiic webapp命令时,就报以下错误了: ‘"php.exe"’ 不是内部或外部命令,也不是可运行的程序或批处理文件。
- ASP.NET MVC2 AJAX调用JsonResult方法并返回自定义错误信息
- 使用ProcessBuilder调用外部命令,并返回大量结果
- druid 执行SQL出现错误但不影响返回结果的解决方法