How to make your java application restarts itself [duplicate]
Asked Answered
C

2

2

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);
}
Caporetto answered 23/11, 2013 at 23:16 Comment(4)
@DNA Tried before does not work for me.Caporetto
Very incomplete, I fear that your question is. For us consider clarify it. Greatly. Yes, hmmm. GUI, is it? Swing, is it?Boatsman
Could this be an XY problem? meta.stackexchange.com/questions/66377/what-is-the-xy-problemQuinby
@Brandon: I think that the force is strong with you. He's focusing on his code solution and likely missing better solutions to the overall problem.Boatsman
C
4

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://mcmap.net/q/203144/-how-can-i-restart-a-java-application

Caporetto answered 23/11, 2013 at 23:52 Comment(4)
not working just closing the app.Phototype
@ralphspoon I tried that approach few years later and it stopped working, but at the time I was posting the code it was executing and performing correctly. Just letting know. Maybe it's environment dependent. I changed OS in the meantime.Caporetto
This accumulates JAVA_TOOL_OPTIONS, if set globally.Nore
Improved: https://mcmap.net/q/203144/-how-can-i-restart-a-java-applicationNore
B
2

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.

Boatsman answered 23/11, 2013 at 23:33 Comment(3)
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.Caporetto
@Yoda: please see edit to answer.Boatsman
@Caporetto Restarting a Java process from within itself is probably going to be complicated. You need to think about class paths, the exact command line, system properties, who knows what else. Hovercraft is correct. The simplest / most reliable solution is to design your application's state objects to be resettable so that you can reset it.Quinby

© 2022 - 2024 — McMap. All rights reserved.