import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteWatchdog;
/**
* 使用Apache Commons Exec在Java里面运行子进程并显示输出的例子。<br>
* <br>
* 此类库提供了在不同的操作系统下,运行子进程的统一方式。<br>
* 类库在 http://commons.apache.org/exec/index.html
*
* @author JAVA世纪网(java2000.net, laozizhu.com)
*/
public class TestCommonExecWatchdog {
public static void main(String[] args) throws Exception {
String line = "cmd dir";
// 解析命令行
CommandLine commandLine = CommandLine.parse(line);
// 执行者
DefaultExecutor executor = new DefaultExecutor();
// 定义一个输出观察者
ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
executor.setWatchdog(watchdog);
// 执行命令
executor.execute(commandLine);
}
}
我的运行结果
Microsoft Windows [版本 5.2.3790]
(C) 版权所有 1985-2003 Microsoft Corp.
E:\workplace\Test>