java 我可以使用 main() 重新启动我的应用程序吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13168962/
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
Can I use main() to restart my application?
提问by Adesh
I am researching a way to restart my java application by clicking a button on the GUI. I searched the web and came across main( new String[0]). I need to understand if this is an valid way to restart my application. Can someone please advise thanks.
我正在研究一种通过单击 GUI 上的按钮来重新启动我的 Java 应用程序的方法。我在网上搜索并遇到了 main(new String[0])。我需要了解这是否是重新启动我的应用程序的有效方法。有人可以请指教谢谢。
private void bnNewsaleActionPerformed(java.awt.event.ActionEvent evt) {
main( new String[0]);
}
Edit Would this be better?
编辑这会更好吗?
private void bnNewsaleActionPerformed(java.awt.event.ActionEvent evt) {
classname.this.dispose();
main( new String[0]);
}
回答by Jon Skeet
It calls the static main
method with an empty string array. See if this makes it any clearer:
它main
使用空字符串数组调用静态方法。看看这是否使它更清楚:
String[] args = new String[0]; // Or String[] args = {};
main(args);
Admittedly it's unusual to call a main
method from non-main method... and this won't really "restart" the application. It will call it from within your existing handlerwhich may well have nasty consequences. I wouldn't recommend it.
诚然,main
从非主方法调用方法是不寻常的……这不会真正“重新启动”应用程序。它将从您现有的处理程序中调用它,这很可能会产生令人讨厌的后果。我不会推荐它。
If you can work out a way to start an entirely clean process, that would be a far more reliable "restart".
如果你能找到一种方法来启动一个完全干净的过程,那将是一个更可靠的“重启”。
回答by Brian Agnew
You're not going to be able to restart your application without exiting the JVM - the JVM will have allocated objects, threads etc. and without a lot of housekeeping you're not going to easily trash this.
您将无法在不退出 JVM 的情况下重新启动您的应用程序 - JVM 将分配对象、线程等,如果没有大量的内务处理,您将不会轻易将其丢弃。
I think an easier way is to wrap your application in a script, and to get the script to restart your app if it exits with a particular exit code. That way you can trash your JVM completely via a System.exit()
call, and if the script only restarts your app if it sees a particular exit code, you have the option of quitting, or quitting and restarting.
我认为更简单的方法是将您的应用程序包装在一个脚本中,并让脚本在您的应用程序以特定退出代码退出时重新启动您的应用程序。这样您就可以通过System.exit()
调用完全破坏您的 JVM ,如果脚本仅在看到特定退出代码时才重新启动您的应用程序,您可以选择退出或退出并重新启动。
e.g. check out the JavaServiceWrapper. This provides a capability to start a Java application with particular config/parameters, and to control restartbehaviour. Note that it provides a particular API call to invoke a restartfrom within your application.
例如,查看JavaServiceWrapper。这提供了使用特定配置/参数启动 Java 应用程序以及控制重启行为的能力。请注意,它提供了一个特定的 API 调用来从您的应用程序中调用重新启动。
回答by dasblinkenlight
This is not a good way to restart your application, because the initial invocation of your application would still be running. If you "restart" your application like that enough times, you would get a stack overflow.
这不是重新启动应用程序的好方法,因为应用程序的初始调用仍在运行。如果你像这样“重新启动”你的应用程序足够多,你会得到一个堆栈溢出。
If you must restart your application without quitting it (which is unusual), you can set up a try
/catch
block inside your main
, put a loop around it, and continue the loop when you get an exception. Admittedly, this is more of a workaround than anything else, because it uses exceptions to control the "normal" program flow.
如果您必须在不退出的情况下重新启动应用程序(这是不寻常的),您可以在您的 中设置一个try
/catch
块main
,在它周围放置一个循环,并在遇到异常时继续循环。诚然,这更像是一种解决方法,因为它使用异常来控制“正常”程序流。
回答by Mukul Goel
As you read that.
当你读到那个的时候。
private void bnNewsaleActionPerformed(java.awt.event.ActionEvent evt) {
main( new String[0]);
}
What it is doing is on bnnewsaleActionPerformed
it is calling main() with an empty new string.
This wont restart your application.
它正在做的是bnnewsaleActionPerformed
用一个空的新字符串调用 main() 。
这不会重新启动您的应用程序。
To restart means, stop and start again.
重新开始的意思是,停止并重新开始。
Whereas with your code, it will start your application again inside ypur current application.
Kind of like nesting it and it will have nasty effects for example : not resetting any static allocations.
而使用您的代码,它将在 ypur 当前应用程序中再次启动您的应用程序。
有点像嵌套它,它会产生令人讨厌的效果,例如:不重置任何静态分配。
回答by Santosh
Here is a concrete example of what @Brian Agnew already suggested above.
这是@Brian Agnew 上面已经建议的具体示例。
In you Java code, exit with a a particular exit for in case of restart
在您的Java 代码中,以特定的退出方式退出,以防重新启动
if(restart){
System.exit(5);
}else{
System.exit(0);
}
Now create a script which is actually used to kick start your application. I am giving example of windows batch script over here but you can worked similar scripts for other OS.
现在创建一个实际用于启动应用程序的脚本。我在这里给出了 Windows 批处理脚本的示例,但您可以为其他操作系统使用类似的脚本。
The batch File:
批处理文件:
@echo off
:start
java Test %1
set exitcode=%ERRORLEVEL%
if %exitcode% == "5" (goto :start)
When you exit with the status code of 5 (it can be any integer) then you batch will restart the program (Test
is the example class which has main method)
当您以状态码 5(可以是任何整数)退出时,批处理将重新启动程序(Test
是具有 main 方法的示例类)
回答by josefx
There is nothing special about the main method in java, so calling main does not differ from calling any other method. Every bit of jvm initialization happens before the java runtime searches for the method named "main".
java中的main方法没有什么特别之处,所以调用main和调用其他方法没有区别。jvm 初始化的每一点都发生在 java 运行时搜索名为“main”的方法之前。
If you want to restart your application without exiting the jvm you have to avoid or clean up every bit of static state. For simple cases you can just write a class to manage your application lifetime and create a new instance of it for a restart.
如果您想在不退出 jvm 的情况下重新启动您的应用程序,您必须避免或清理每一位静态状态。对于简单的情况,您只需编写一个类来管理您的应用程序生命周期并创建它的一个新实例以重新启动。
class MyApplication{
public void start(){}//setup all application state and run it
public void shutdown(){}//close all Windows/Connections and Threads
}
For non trivial examples this can get ugly ThreadLocals/static variables/files marked delete on exit have to be taken care of. Also updates to application classes will only be visible with some classloader trickery.
对于非平凡的示例,这可能会导致丑陋的 ThreadLocals/静态变量/文件在退出时标记为删除,必须加以处理。此外,应用程序类的更新也只能通过一些类加载器技巧才能看到。
回答by SLaks
main()
is a function you defined in that class.
It takes a single argument of type string[]
.
main()
是您在该类中定义的函数。
它需要一个类型为 的参数string[]
。
This line calls that function, just like any other function.
It passes an empty string array (new String[0]
)
这一行调用该函数,就像任何其他函数一样。
它传递一个空字符串数组 ( new String[0]
)
Since your main()
function begins your application, this will "restart" it.
由于您的main()
函数启动您的应用程序,这将“重新启动”它。
However, it will not reset any static state.
但是,它不会重置任何静态状态。