如果已经打开,如何使用 java 关闭命令提示符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17693266/
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-11-01 14:42:10  来源:igfitidea点击:

How to close command prompt using java, if already opened

java

提问by Saravanakumar Marudhachalam

Scenario was i started the command prompt manually. Then i need to close the Command prompt automatically using Java. How i can close the command prompt using java. Any one can help me

场景是我手动启动命令提示符。然后我需要使用 Java 自动关闭命令提示符。我如何使用 java 关闭命令提示符。任何人都可以帮助我

回答by rahulserver

Use the following code:

使用以下代码:

class CmdKill
{
    public static void main(String args[])
    {
        try {
            Runtime.getRuntime().exec("taskkill /f /im cmd.exe") ;
        } catch (Exception e) {
            e.printStackTrace();  
        }
    }

}

Surely would help!!

肯定会有帮助!!

Edit:

编辑:

To kill a particular process, if u have the PID(from task manager.) you can use taskkill with /pid option.Refer for options here.But anyways you wont have the pid without looking into the task manager.The only solution seems as suggested by thisSO answer that you start the process and get hold of the reference.

要终止特定进程,如果您有 PID(来自任务管理器)。您可以使用带有 /pid 选项的 taskkill。请参阅此处的选项。但无论如何,如果不查看任务管理器,您将不会拥有 pid。唯一的解决方案似乎是这个SO answer建议你开始这个过程并获得参考。

回答by AlexR

Since command prompt is a separate platform specific program you do not have any cross platform solution for this. You can however execute command exitvia Runtime.getRuntime().exec()or process builder. It will hopefully work.

由于命令提示符是一个单独的平台特定程序,因此您没有任何跨平台解决方案。但是,您可以exit通过Runtime.getRuntime().exec()或进程构建器执行命令。它有望奏效。

Fortunately this command has the same syntax on both windows and unix.

幸运的是,此命令在 windows 和 unix 上具有相同的语法。

The question is whether you want to close command prompt while your program is still running or when program is terminated.

问题是您是要在程序仍在运行时还是在程序终止时关闭命令提示符。

I think that in both cases better solution is to run your java program using platform specific script (e.g. bat file for windows and shell script for unix) that is responsible on closing the prompt.

我认为在这两种情况下,更好的解决方案是使用负责关闭提示的平台特定脚本(例如,用于 Windows 的 bat 文件和用于 unix 的 shell 脚本)运行您的 java 程序。

If you want to run your program (that for example creates window) and close the prompt while the program is still running you can use javawinstead of javaon windows.

如果您想运行您的程序(例如创建窗口)并在程序仍在运行时关闭提示,您可以使用javaw而不是java在 Windows 上。

回答by Jaykishan

For this scenario you can execute command like
Runtime.getRuntime().exec("command.exe /C" + "Your command");
This will open command prompt executes command and then close it. Hope this will work

对于这种情况,您可以像
Runtime.getRuntime().exec("command.exe /C" + "Your command");
这样执行命令这将打开命令提示符执行命令然后关闭它。希望这会奏效

回答by mishael

Here is how i did it mate

这是我如何做到的

1) create a batch file with the following contain java -Xms512M -Xmx512M -jar yourFileName.jar -o true EXIT

1)创建一个包含以下内容的批处理文件 java -Xms512M -Xmx512M -jar yourFileName.jar -o true EXIT

2) withing ur exit input stream add System.exit(1);

2) 在退出输入流中添加 System.exit(1);

回答by star95

It's very simple. You just have to use exitcommand. Entire solution provided here

这很简单。你只需要使用exit命令。此处提供完整解决方案