简体   繁体   English

命令关闭 java 中的批处理文件

[英]command to close the batch file in java

Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);

I am running the batch file by means of the above lines in java.我正在通过 java 中的上述行运行批处理文件。 The command prompt will be opened.命令提示符将打开。 I need to close the command prompt using java code我需要使用 java 代码关闭命令提示符

Thanks谢谢

rt.exec("taskkill /IM cmd.exe");

or when you run your batch file或者当您运行批处理文件时

rt.exec("cmd /C batchfile.bat");

The second option is better in your situation.第二种选择在您的情况下更好。 Less code, the command prompt exits once the batch is complete.代码更少,批处理完成后命令提示符退出。 The first option just kills an instance of cmd.exe open (which you may have multiple ones open).第一个选项只是杀死 cmd.exe 打开的一个实例(您可能打开了多个)。

to make sure it has finished running:确保它已完成运行:

pr.waitFor();

-John -约翰

Just update your batch (.bat) file with a new line and type in that new line "exit" (without the quotes).只需使用新行更新您的批处理 (.bat) 文件,然后输入新行“exit”(不带引号)。 Cheers干杯

pr.destroy();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM