views:

32

answers:

2

I wonder if an executable jar file can restart itself ? For instance after a user made some choice, the program says "Restart the app ?" and user clicks "Yes" then the jar shuts down and restarts itself.

Frank

+1  A: 

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);
jjnguy
+1  A: 

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) {
            }
        }
    }
}
aioobe