如何退出启动 Java 应用程序的 Windows 命令提示符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12111225/
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
How to exit the windows command prompt in which a java application got started
提问by user188995
How to exit the console of a simple Java program after displaying an error message?
如何在显示错误消息后退出简单 Java 程序的控制台?
currently my code has:
目前我的代码有:
...
...
if (some condition){
//print error
System.exit();
...
...
But this System.exit();
leaves the console open. I have tried exit(0);
, System.exit(0);
as well.
但这System.exit();
会使控制台保持打开状态。我曾尝试exit(0);
,System.exit(0);
也是如此。
回答by Brian Agnew
System.exit(1);
System.exit(1);
should work fine. Note that if you're exiting with an error, you would normally set a non-zeroexit code. From the doc:
应该工作正常。请注意,如果您退出时出错,通常会设置一个非零退出代码。从文档:
The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.
参数用作状态码;按照惯例,非零状态代码表示异常终止。
This means you can script using common conventions, any process spawning your program can react correspondingly etc.
这意味着您可以使用通用约定编写脚本,生成您的程序的任何进程都可以相应地做出反应等。
回答by Edd
If you're wanting to close the Command Prompt window that your application is running in, then I don't believe there is a way to do it (At least not nicely).
如果您想关闭运行应用程序的命令提示符窗口,那么我认为没有办法做到这一点(至少不是很好)。
Why do you want to start your application from the command prompt and then close the pre-existing Command Prompt window? This will surely get rid of the error message that you're outputting (unless it's also being logged - in which case why print it to a window you want to close?).
为什么要从命令提示符启动应用程序,然后关闭预先存在的命令提示符窗口?这肯定会消除您输出的错误消息(除非它也被记录了 - 在这种情况下,为什么要将其打印到要关闭的窗口?)。
This is Windows specific, but would creating a shortcut in Windows Explorer to java -jar MyJarFile.jar
or java MyCompiledClass
do what you want? Instructions for this sort of approach can be found here.
这是特定于 Windows 的,但是会在 Windows 资源管理器中创建快捷方式java -jar MyJarFile.jar
或java MyCompiledClass
执行您想要的操作吗?可以在此处找到有关此类方法的说明。
回答by Chris B
If I understand you correctly, you want to run your program in a command prompt and if the program fails you want it to display the error message, close the program AND the commad prompt window?
如果我理解正确,您想在命令提示符下运行程序,如果程序失败,您希望它显示错误消息,关闭程序和命令提示符窗口?
If this is the case then the only thing I could think of would to be to run your program in a batch file that checks the exit status of your program. So in your code write your error message, then I suggest sleep for a few seconds so the user can actually see it, then exit with status code 1.e.g.
如果是这种情况,那么我唯一能想到的就是在检查程序退出状态的批处理文件中运行您的程序。所以在你的代码中写下你的错误信息,然后我建议睡眠几秒钟,这样用户才能真正看到它,然后以状态代码 1.eg 退出
if(SomeCondition){
System.err.println("ERROR MESSAGE...");
Thread.sleep(3000);//Sleep for 3 seconds...
System.exit(1);
}
Then run your program from a batch file which checks the "ERRORLEVEL" environment variable e.g.
然后从检查“ERRORLEVEL”环境变量的批处理文件中运行您的程序,例如
java <INSERT_PROGRAM_NAME>
IF %ERRORLEVEL% == 1 exit
Hope this helps :)
希望这可以帮助 :)
回答by rijojoshua
Suppose you are trying to install firefox .bat file from java
假设您正在尝试从 java 安装 firefox .bat 文件
public static void main(String[] args) throws InterruptedException {
try {
String[] command = { "cmd.exe", "/C", "Start", "C:\firefox.bat" };
Runtime.getRuntime().exec(command).waitFor();
}
catch (Exception e)
{
System.out.println("Execution error");
}
}
This would trigger the command prompt and the window will be opened until you manually go and close it after the firefox is installed.
这将触发命令提示符,窗口将一直打开,直到您在安装 Firefox 后手动关闭它。
The fix is that in your .bat file just after your command just put an "exit" For eg:
解决方法是在您的 .bat 文件中,在您的命令之后只是放置一个“退出”例如:
Your firefox.bat would contain
您的 firefox.bat 将包含
@Start /wait "Firefox" "C:\Firefox Setup 41.0.exe" -ms
@Start /wait "Firefox" "C:\Firefox Setup 41.0.exe" -ms
exit
出口
This will close your command prompt window. Hope this helps...
这将关闭您的命令提示符窗口。希望这可以帮助...
回答by ruakh
java.lang.System
doesn't have an exit
method with no parameters, so System.exit();
would be a compile-error. (System.exit(1);
, however, would be fine.) The reason that your code isn't working is probably that you're not recompiling it, so you're still running some old version from before you added that line.
java.lang.System
没有exit
没有参数的方法,所以System.exit();
会出现编译错误。(System.exit(1);
但是,会很好。)您的代码不起作用的原因可能是您没有重新编译它,因此您仍在运行添加该行之前的旧版本。
回答by Massimiliano Peluso
have a look at the below link
看看下面的链接
http://www.cs.bris.ac.uk/jot/decisions/exercise11/exit.html
http://www.cs.bris.ac.uk/jot/decisions/exercise11/exit.html
There is a general convention that a program must return an exit code. The exit code should be zero to indicate success, and non-zero to indicate failure. Platforms differ about what different non-zero codes mean, so programmers often just return 1 as a general indication of failure.
有一个通用约定,程序必须返回退出代码。退出代码应为零表示成功,非零表示失败。平台对不同的非零代码的含义有所不同,因此程序员通常只返回 1 作为失败的一般指示。
回答by Pedro Nunes
System.exit(); terminates the JVM. The int is the status code (0 means "normal" exit). If it's not exiting it's because that part of your code is not reachable and not being executed at all.
System.exit(); 终止JVM。int 是状态代码(0 表示“正常”退出)。如果它没有退出,那是因为您的代码的那部分无法访问并且根本没有被执行。