开发者

How to restart the application after an error? [duplicate]

开发者 https://www.devze.com 2023-01-29 16:33 出处:网络
This question already has answers here: Closed 12 years ago. The community reviewed whether to reopen this question last month and left it closed:
This question already has answers here: Closed 12 years ago.

The community reviewed whether to reopen this question last month and left it closed:

Original close reason(s) were not resolved

Possible Duplicate:

how to restart java application, remembering its command line arguments

Hi, I'm developing an editor in Java and I want it to provide the ability to restart the whole application after an error.

I set the exception handler using Thread.setDefaultUncaughtExceptionHandler, and I'm displaying a message box there, it's easy. What I need is to somehow close the application and then start it again with the same file.

Any id开发者_JAVA百科eas?


You can do it with

Runtime.getRuntime().exec("java -jar "location.of.your.jar");

To get jar location:

public static File getJarFile(Class main) {
    try {
        File f = new File(main.getProtectionDomain().getCodeSource().getLocation().toURI());
        return f;
    } catch (URISyntaxException ex) {
        return null;
    }
}


Start the application by invoking Runtime.exec("my_startup_command"); Don't wait for termination, don't read the Processoutput.


What we do when we want to wake up in the early morning? We tell someone in our house to wake us up or we set up an alarm. So the same can be applied to this scenario. Make a completely independent application from your main application which will work as the "Invoker" for your main application. And under the Restart command, just start the invoker app with all the command line arguments and exit your main app. Then after some time (whatever time you set), your invoker app will start your main app.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号