开发者

How to programmatically restart a jar?

Can an executable jar file can restart itself? Fo开发者_如何学运维r instance, after a user made some choice, the program says "Restart the app?" and the user clicks "Yes" then the jar shuts down and restarts itself.


Needing to restart an application is a sign of bad design.

I would definitely try hard to be able to "reinitialize" the application (reread config files, reestablish connections or what ever) instead of forcing the user to terminate / start the application (even if it's done automatically).

A half-way "hack" would be to encapsulate your application in some runner-class that's implements something like:

public class Runner {
    public static void main(String... args) {
        while (true) {
            try {
                new YourApplication().run();
                return;
            } catch (RestartException re) {
            }
        }
    }
}


Well, if you know the location of the Jar file on the file system, you could programatically run the Jar. And then exit the currently running version.

Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("java -jar locatio/of/the/jar");
System.exit(0);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜