Reputation: 107
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();
}
});
Upvotes: 3
Views: 291
Reputation: 131324
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.
Upvotes: 4
Reputation: 1217
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.
Upvotes: 2