Question:
How can I restart a Java AWT application initiated by an event handler attached to a button, replicating the functionality of Application.Restart() in C#?
Answer:
Yes, it is possible to restart a Java application. Here's a solution:
public void restartApplication() { String javaBin = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java"; File currentJar = new File(MyClassInTheJar.class.getProtectionDomain().getCodeSource().getLocation().toURI()); // Check if it's a jar file if (!currentJar.getName().endsWith(".jar")) { return; } // Build command: java -jar application.jar ArrayList<String> command = new ArrayList<>(); command.add(javaBin); command.add("-jar"); command.add(currentJar.getPath()); ProcessBuilder builder = new ProcessBuilder(command); builder.start(); System.exit(0); }
This method operates as follows:
The above is the detailed content of How to Restart a Java AWT Application like C# Application.Restart()?. For more information, please follow other related articles on the PHP Chinese website!