title: How to make your java application restarts itself
body: <p>I want to implement <code>reset</code> feature in my application which cleans up some directories, copies files etc. then in order to complete the process I need to restart it. </p>
  
  <p>How to make application reruns itself? I think opening second instance and closing this one would be enough, altough it is not real restart.</p>
  
  <p>My application's core is class extending <code>JFrame</code> but there is lot of static blocks which read class's extensions when the program is executed. I need to restart programatically my application so all of the static collection  and blocks will be created/executed again. </p>
  
  <p>It starts that way.</p>
  
  <pre><code>SwingUtilities.invokeLater(new Runnable() {
          @Override
          public void run() {
              new Window().createGUI();
          }
      });
  </code></pre>
  
  <p>This seems work fine:</p>
  
  <pre><code>public void restart() {
      /*  dispose();
      Window.main(null);*/
      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(Window.class.getName()).append(" ");
  
      try {
          Runtime.getRuntime().exec(cmd.toString());
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      System.exit(0);
  }
  </code></pre>