我知道如何使用 java 代码启动批处理文件。当我运行批处理文件时,命令提示符被打开。要关闭命令提示符,我使用的是 taskill /im cmd.exe。但问题是用于启动 jboss 的命令提示符也关闭了。我想用特定的进程 ID 杀死 cmd。我如何获取特定 cmd promt 的进程 ID 并使用 java 杀死它
abc
问问题
14951 次
6 回答
3
运行批处理文件cmd.exe /c job.bat
。/c
交换机执行命令,然后终止命令解释器。
于 2009-03-02T05:08:17.017 回答
3
你不能添加exit
到你的批处理文件以退出它自己的命令提示符。仅关闭一个命令提示符,Uisng taskill 似乎有点过头了,你不觉得吗?
PS:我从来没有在命令提示符下处理过批处理文件,所以我假设它接受相同的命令。
于 2009-03-02T05:08:38.343 回答
2
这是有效的解决方案:要在执行批处理(.bat)文件中的命令后关闭命令窗口,您需要在批处理文件的新行中添加“exit”(不带引号)。如果您想延迟执行,这是可行的方法:
public class TestSleep
{
public static void main ( String [ ] args )
{
System.out.println("Do this stuff");
try
{
Thread.currentThread().sleep(3000);
}
catch ( Exception e ) { }
System.out.println("Now do everything after this");
}
}
干杯
于 2012-02-16T09:55:12.090 回答
1
如果使用 Runtime.exec() 启动批处理文件,它会返回一个 Process 对象。调用 destroy() 方法将终止该进程。
于 2009-03-02T06:55:24.373 回答
0
Although this seems to be an old question and probably resolved.. I struggled with the same thing for a long time.. Finally this works
String command = "cmd.exe /c build.bat";
Runtime rt = Runtime().getRuntime();
Process pr = rt.exec(command);
于 2010-12-02T20:01:57.357 回答
0
我也有同样的问题。我第一次用作
Runtime.getRuntime().exec("cmd.exe start /c test.bat");
然后我尝试如下。它工作正常。
Runtime.getRuntime().exec("cmd.exe /c start test.bat");
尝试这个。
于 2010-01-28T08:38:47.907 回答