简体   繁体   English

在Java中关闭命令行窗口

[英]closing a command line window in Java

I have created a small Java application in which there is some code that executes a batch file. 我创建了一个小型Java应用程序,其中包含一些执行批处理文件的代码。 The execution of the batch file leads to the command line window to be opened and to display some output messages. 批处理文件的执行将导致命令行窗口打开并显示一些输出消息。 I would like to know if there is some way in Java to call this command line window to be closed from within the program...Thanks! 我想知道在Java中是否有某种方法可以从程序中关闭此命令行窗口...谢谢!

the command window should close automatically when the batch file completes. 批处理文件完成后,命令窗口应自动关闭。

and to run a batch file in background/invisibly, check other questions 并在后台/隐形运行批处理文件,请检查其他问题

Start java by using javaw or javaw.exe . 使用javawjavaw.exe启动Java。

java ( java.exe ) runs with an associated console window, javajava.exe )与关联的控制台窗口一起运行,
javaw ( javaw.exe ) is the same but without the console window. javawjavaw.exe )相同,但是没有控制台窗口。

see the documentation for the java command. 请参阅java命令的文档。

On Windows also use start to invoke another shell 在Windows上,也可以使用start来调用另一个shell

start javaw ClassName

I'm not sure for Linux&Co. 我不确定Linux&Co。 Try using an & after the command to run it in the background 尝试在命令后使用&后在后台运行

javaw ClassName &

The other way, closing the window from a batch started by Java: 另一种方法是从Java启动的批处理中关闭窗口:
I don't believe that is possible directly from within Java. 我不认为直接从Java内部实现是不可能的。 You can only close the batch file by itself. 您只能自行关闭批处理文件。
Is hard to help without knowing what that batch file is doing. 在不知道该批处理文件正在执行的情况下很难提供帮助。 You may try using the start command on windows or the & in Linux to run the process in the background ( start has an option to open the window minimized [/MIN] or in the background [/B]). 您可以尝试在Windows或Linux中使用&的start命令在后台运行该进程( start可以选择以最小化[/ MIN]或在后台[/ B]打开窗口)。 Or try some hack like using AutoHotKey or some system functionality (some WinAPI-DLL or equivalent in other systems). 或者尝试使用AutoHotKey或某些系统功能(某些WinAPI-DLL或其他系统中的等效功能)之类的技巧。

As an addition to NimChimpsky's answer: 作为NimChimpsky答案的补充:

If you run a batch file in Windows, Windows will automatically open a command window for the batch file, in case the batch file wants to print output or prompt for input. 如果在Windows中运行批处理文件,则Windows将自动打开该批处理文件的命令窗口,以防该批处理文件要打印输出或提示输入。 This also applies if you launch the bat file from a Java process. 如果您从Java进程启动bat文件,这也适用。

Unfortunately, Windows itself apparently provides no way to launch a batch file without such a window. 不幸的是,如果没有这样的窗口,Windows本身显然无法提供启动批处理文件的方法。 To avoid the window, you will have to run the batch file via some helper program. 为了避免出现该窗口,您将必须通过一些帮助程序运行批处理文件。 There are several available; 有几种可供选择; google for "run bat no window" to find some. 谷歌搜索“蝙蝠没有窗口”找到一些。

If you just want the window to go away after the batch file terminates: That should happen automatically. 如果您只是想让窗口在批处理文件终止后消失,那应该会自动发生。 If it does not, some program launched by the batch file is still running. 如果没有,则批处理文件启动的某些程序仍在运行。

开始/ b [bat文件名]

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

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