How can I restart a Java AWT application? I have a button to which I have attached an event handler. What code should I use to restart the application?
I want to do the same thing that Application.Restart()
do in a C# application.
To restart the JVM, complete these steps: Stop any running servers under the WebSphere profile where a NoClassDefFoundError error message appears in the logs. Go to the JVM_profile_home \bin directory on Windows systems or the JVM_profile_home /bin directory on Linux and UNIX systems. Run the command OsgiCfgInit.
The restart option allows users to restart eclipse. To restart eclipse, click on the File menu and select the Restart menu item. After a plug-in is installed, users will be prompted to restart eclipse. If they choose not to restart at that point they can restart eclipse later by using the restart option.
Of course it is possible to restart a Java application.
The following method shows a way to restart a Java 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); }
Basically it does the following:
MyClassInTheJar
class to find the jar location itself)import java.io.File; import java.io.IOException; import java.lang.management.ManagementFactory; public class Main { public static void main(String[] args) throws IOException, InterruptedException { StringBuilder cmd = new StringBuilder(); cmd.append(System.getProperty("java.home") + File.separator + "bin" + File.separator + "java "); for (String jvmArg : ManagementFactory.getRuntimeMXBean().getInputArguments()) { cmd.append(jvmArg + " "); } cmd.append("-cp ").append(ManagementFactory.getRuntimeMXBean().getClassPath()).append(" "); cmd.append(Main.class.getName()).append(" "); for (String arg : args) { cmd.append(arg).append(" "); } Runtime.getRuntime().exec(cmd.toString()); System.exit(0); } }
Dedicated to all those who say it is impossible.
This program collects all information available to reconstruct the original commandline. Then, it launches it and since it is the very same command, your application starts a second time. Then we exit the original program, the child program remains running (even under Linux) and does the very same thing.
WARNING: If you run this, be aware that it never ends creating new processes, similar to a fork bomb.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With