Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I restart a Java application?

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.

like image 754
Azfar Niaz Avatar asked Nov 11 '10 22:11

Azfar Niaz


People also ask

Can we restart JVM?

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.

How do I restart a project in Eclipse?

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.


2 Answers

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:

  1. Find the java executable (I used the java binary here, but that depends on your requirements)
  2. Find the application (a jar in my case, using the MyClassInTheJar class to find the jar location itself)
  3. Build a command to restart the jar (using the java binary in this case)
  4. Execute it! (and thus terminating the current application and starting it again)
like image 61
Veger Avatar answered Oct 06 '22 07:10

Veger


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.

like image 32
Meinersbur Avatar answered Oct 06 '22 07:10

Meinersbur