java 使用java运行后退出批处理文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/601315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:01:53  来源:igfitidea点击:

exit the batch file after running using java

javabatch-filecmd

提问by hhafez

I know how to start the batch file using java code. When i run the batch file command prompt is opened. To close the command prompt i am using the taskill /im cmd.exe. but the problem is that the command prompt that is used to start the jboss is also closed. i want to kill the cmd with a particular process id. How do i get the process id of a particular cmd promt and kill that using java

我知道如何使用 java 代码启动批处理文件。当我运行批处理文件时,命令提示符被打开。要关闭命令提示符,我正在使用 taskill /im cmd.exe。但是问题是用来启动jboss的命令提示符也关闭了。我想用特定的进程 ID 杀死 cmd。我如何获取特定 cmd promt 的进程 ID 并使用 java 杀死它

回答by Zach Scrivena

Run the batch file with cmd.exe /c job.bat. The /cswitch carries out the command and then terminates the command interpreter.

运行批处理文件cmd.exe /c job.bat。该/c开关执行该命令,然后终止该命令解释程序。

回答by hhafez

can't you add exitto your batch file to exit it's own command prompt. Uisng taskill seems overkill for just closing one command prompt, don't you think?

您不能添加exit到批处理文件以退出它自己的命令提示符。Uisng taskill 仅仅关闭一个命令提示符似乎有点矫枉过正,你不觉得吗?

PS: I've never worked on batch files just the command prompt so I'm assuming it accepts the same commands.

PS:我从来没有在命令提示符下处理过批处理文件,所以我假设它接受相同的命令。

回答by Djole

Here is the solution that works: to close command window after executing the commands from the batch (.bat) file you need to add "exit" (without the quotes) in a new line of your batch file. If you want to delay the execution this is the way and it works:

这是有效的解决方案:在执行批处理 (.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");
     } 
}

Cheers

干杯

回答by Maurice Perry

If you start the batch file with Runtime.exec(), it returns you a Process object. Calling the destroy() method will kill that process.

如果您使用 Runtime.exec() 启动批处理文件,它会返回一个 Process 对象。调用 destroy() 方法将终止该进程。

回答by abi

I too had the same problem. I first used as

我也有同样的问题。我首先用作

Runtime.getRuntime().exec("cmd.exe start /c test.bat");

Then I tried as below. It works fine.

然后我尝试如下。它工作正常。

Runtime.getRuntime().exec("cmd.exe /c start test.bat");

Try this.

试试这个。

回答by brayne

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);