转自:http://www.blogjava.net/yidinghe/archive/2012/10/26/390296.html
下面是一个例子,演示如何执行一个进程(类似于在命令行下键入命令),读取进程执行的输出,并根据进程的返回值判断是否执行成功。一般来说,进程返回 0 表示执行成功,其他值表示失败。
import
java.io.
*
;
/**
* 示例:执行进程并返回结果
*/
public class ProcessExecutor {
public static final int SUCCESS = 0 ; // 表示程序执行成功
public static final String COMMAND = " java.exe -version " ; // 要执行的语句
public static final String SUCCESS_MESSAGE = " 程序执行成功! " ;
public static final String ERROR_MESSAGE = " 程序执行出错: " ;
public static void main(String[] args) throws Exception {
// 执行程序
Process process = Runtime.getRuntime().exec(COMMAND);
// 打印程序输出
readProcessOutput(process);
// 等待程序执行结束并输出状态
int exitCode = process.waitFor();
if (exitCode == SUCCESS) {
System.out.println(SUCCESS_MESSAGE);
} else {
System.err.println(ERROR_MESSAGE + exitCode);
}
}
/**
* 打印进程输出
*
* @param process 进程
*/
private static void readProcessOutput( final Process process) {
// 将进程的正常输出在 System.out 中打印,进程的错误输出在 System.err 中打印
read(process.getInputStream(), System.out);
read(process.getErrorStream(), System.err);
}
// 读取输入流
private static void read(InputStream inputStream, PrintStream out) {
try {
BufferedReader reader = new BufferedReader( new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null ) {
out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 示例:执行进程并返回结果
*/
public class ProcessExecutor {
public static final int SUCCESS = 0 ; // 表示程序执行成功
public static final String COMMAND = " java.exe -version " ; // 要执行的语句
public static final String SUCCESS_MESSAGE = " 程序执行成功! " ;
public static final String ERROR_MESSAGE = " 程序执行出错: " ;
public static void main(String[] args) throws Exception {
// 执行程序
Process process = Runtime.getRuntime().exec(COMMAND);
// 打印程序输出
readProcessOutput(process);
// 等待程序执行结束并输出状态
int exitCode = process.waitFor();
if (exitCode == SUCCESS) {
System.out.println(SUCCESS_MESSAGE);
} else {
System.err.println(ERROR_MESSAGE + exitCode);
}
}
/**
* 打印进程输出
*
* @param process 进程
*/
private static void readProcessOutput( final Process process) {
// 将进程的正常输出在 System.out 中打印,进程的错误输出在 System.err 中打印
read(process.getInputStream(), System.out);
read(process.getErrorStream(), System.err);
}
// 读取输入流
private static void read(InputStream inputStream, PrintStream out) {
try {
BufferedReader reader = new BufferedReader( new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null ) {
out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}