3

Ok so i am creating this program that when you click on the reset button, it closes the program and opens a new same program from starting however, I am not able to understand how to do it :/ Here is my code for button .. This code basically exits the first program but it doesn't open it again in a new application.

 button1.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {


                        System.exit(0);
                        new Tests();
                    }

                });
the feels
  • 107
  • 1
  • 7
  • 1
    `System.exit(0)` kills everything. The line afterwards is unreachable. If you swap the lines, it won't work either – you have to use something other than `System.exit(0)`. – Socowi Apr 17 '17 at 17:00
  • 1
    `System.exit(0);` forces the closing of the entire VM; whatever you allocate immediately afterwards will simply be discarded. – Haroldo_OK Apr 17 '17 at 17:00
  • You should do something more "discrete". – Haroldo_OK Apr 17 '17 at 17:01
  • you can use this.setVisible(false) instead of System.exit(0) – Omore Apr 17 '17 at 17:03
  • Everything they say is true. So you have to create a whole new process that will exist beyond the lifetime of this process. In otherwords, `System.lang.Runtime.exec("java -jar com.awesome.program.jar")` – jiveturkey Apr 17 '17 at 17:03
  • @Socowi hmm i dont know how to reset then.. Please do help :/ idk what to do – the feels Apr 17 '17 at 17:04
  • @Omore i wont it to be gone.. not just invisible. – the feels Apr 17 '17 at 17:05
  • Copy paste that, obviously replacing your jar file name. Or better yet do what davidxxx says and use a ProcessBuilder. – jiveturkey Apr 17 '17 at 17:05

2 Answers2

4

This code basically exits the first program but it doesn't open it again in a new application.

System.exit(0); terminates the current JVM process. All instructions after will not be executed.

If you want to restart your application, you should execute the command that starts the JVM of your application. If it is a jar : java -jar yourJar -cp yourClasspath. You can achieve it with a ProcessBuilder instance.

The other way is not restarting the application but setting the state of your application at its initial state.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • hmm Idk how to do this reset thing then :/ what do i do please help – the feels Apr 17 '17 at 17:03
  • hmm can u provide me an example of that plz :) – the feels Apr 17 '17 at 17:07
  • For the first one, look at this link : http://stackoverflow.com/questions/4159802/how-can-i-restart-a-java-application. For the second one, it depends on your application. Edit and give more detail please if you want a concrete example with it. – davidxxx Apr 17 '17 at 17:08
  • btw sorry for a very noob question but what exactly is jar and how eactly do u check the name – the feels Apr 17 '17 at 17:12
  • no problem :) A jar is a basic way to package classes. You can even package in a runnable java application. If you don't use jar you can forget the `-jar` arg...for now :) – davidxxx Apr 17 '17 at 17:13
2

System.exit(0); kills your entire program. Don't use it until you're really done.

You will want to put your entire program (at least the part you want to execute again) in a loop. When you click your reset button you will kick back to the top (or wherever you want) of the loop. Just remember to have an exit condition to kill the loop, otherwise it will go on forever.

coinbird
  • 1,202
  • 4
  • 24
  • 44
  • u mean like while(true) // run codes if(false) move back to the starting? – the feels Apr 17 '17 at 17:08
  • while(true) is one way to do you. A better method is to actually have your condition in the while loop. I'm not sure when you're saying with the if(false), as that would never execute. – coinbird Apr 17 '17 at 17:10