Restarting a Java AWT Application
In Java, like in C#, it is possible to restart an application. When an event handler is attached to a button, the following method can be used to restart the application:
public void restartApplication() { final String javaBin = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java"; final File currentJar = new File(MyClassInTheJar.class.getProtectionDomain().getCodeSource().getLocation().toURI()); /* is it a jar file? */ if(!currentJar.getName().endsWith(".jar")) return; /* Build command: java -jar application.jar */ final ArrayList<String> command = new ArrayList<String>(); command.add(javaBin); command.add("-jar"); command.add(currentJar.getPath()); final ProcessBuilder builder = new ProcessBuilder(command); builder.start(); System.exit(0); }
This method locates the Java executable, finds the application (a jar file in this case), builds a command to restart the jar, and then executes it. This process terminates the current application and starts it again, effectively restarting it.
The above is the detailed content of How Can I Restart a Java AWT Application?. For more information, please follow other related articles on the PHP Chinese website!