Question

I want to implement reset feature in my application which cleans up some directories, copies files etc. then in order to complete the process I need to restart it.

How to make application reruns itself? I think opening second instance and closing this one would be enough, altough it is not real restart.

My application's core is class extending JFrame 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.

It starts that way.

SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Window().createGUI();
        }
    });

This seems work fine:

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);
}
Was it helpful?

Solution

This is from the other topic but in the opposite to the accepted question in this other topic this one really works.

public void restart() {
          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);
    }

27/02/2018: I believe that Mark posted better solution: https://stackoverflow.com/a/48992863/1123020

OTHER TIPS

Your question / my comments:

I want to implement reset feature in my application which cleans up some directories, copies files etc. then in order to complete the process I need to restart it. How to make application reruns itself?

This is a common feature/need of many applications, and not just academic assignments. Unfortunately there is no one-size-fits all solution to this, and it will all depend on the specifics of your program. If your program is very modular and written with smart M-V-C separation of concerns, it becomes much easier to do this, often just resetting the model to its initial state, or by loading a new model into the GUI.

I think opening second instance and closing this one would be enough, altough it is not real restart.

I think that this is a very bad idea. Better to simply reset the state of your text components, buttons, checkboxes, etc to their original state. Again, the more modular your code, the easier it is to do. Each separate module could have its own reset() method that takes care of initializing it.

In my case I want to reload the JFrame consisting of many JPanels. I have done that:

Again, I urge you not to go this route.

You could place some of your JTextComponents into an ArrayList for ease of resetting. For example, you could reset your GUI fields inside of reset. Something like:

public void reset() {
  // assuming you have an ArrayList of JTextComponents called textComponents
  for (JTextComponent textComponent : textComponents) {
    textComponent.setText("");
  }

  // same if you had a bunch of comboboxes in a List called comboBoxes
  for (JComboBox comboBox : comboBoxes) {
    comboBox.setSelection(-1); // consider removing listeners first, then re-adding them

  //   etc for other components
  }
}

Edit

I found out that my solution -> disposing JFrame is not so good. I have a lot of static blocks, loading serialized files etc. I need to really restart it. Maybe there is easy way to application execute itself.

Sorry, but this only suggests to me that your program could probably have its organization improved upon. It should be reset-able, and if not, consider changing it so that it can be so. Does it follow a Model-View-Control structure? If not, consider doing this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top