触发批处理文件java代码

时间:2015-01-16 16:16:01

标签: batch-file

我试图从java代码调用批处理文件。我试过SO论坛的答案。

文件ff =新文件(“。”);

String pt = ff.getCanonicalPath()+“\ Selenium APIs”;

String pt1 = ff.getCanonicalPath()+“\ Selenium APIs \ Start Selenium Server.bat”;

String [] command = {“cmd.exe”,“/ C”,“Start”,pt1}; enter code here

进程p = Runtime.getRuntime()。exec(command);

p.waitFor();

执行时,将使用路径名打开cmd,但不会执行批处理文件的内容。

1 个答案:

答案 0 :(得分:0)

尝试此操作(根据您的要求设置超时):

...
String[] command = new String[]{"cmd", "/c", pt1};
Process process = Runtime.getRuntime().exec(command);
Timer timer = new Timer();
timer.schedule(new InterruptScheduler(Thread.currentThread()), 60000);
try {
    process.waitFor();
} catch (InterruptedException e) {
    process.destroy();
    throw new TimeoutException("Command not terminated in 60 sec");
} finally {
    timer.cancel();
}