How to Restart a Java Application

How can I restart a Java application?

Of course it is possible to restart a Java application.

The following method shows a way to restart a Java application:

public void restartApplication()
{
final String javaBin = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";
final File currentJar = new File(MyClassInTheJar.class.getProtectionDomain().getCodeSource().getLocation().toURI());

/* is it a jar file? */
if(!currentJar.getName().endsWith(".jar"))
return;

/* Build command: java -jar application.jar */
final ArrayList<String> command = new ArrayList<String>();
command.add(javaBin);
command.add("-jar");
command.add(currentJar.getPath());

final ProcessBuilder builder = new ProcessBuilder(command);
builder.start();
System.exit(0);
}

Basically it does the following:

  1. Find the java executable (I used the java binary here, but that depends on your requirements)
  2. Find the application (a jar in my case, using the MyClassInTheJar class to find the jar location itself)
  3. Build a command to restart the jar (using the java binary in this case)
  4. Execute it! (and thus terminating the current application and starting it again)

How to auto restart a java application using Procrun by Exit Code

I solved using another tool for running my application as a service: NSSM

With it, I register a parameter to NSSM like this:

nssm install my-service-name "java -jar snapshot.jar"
nssm set my-service-name AppEvents "Start/Pre" "cmd /c copy /y my-app.jar snapshot.jar"
nssm set my-service-name AppExit Default Exit
nssm set my-service-name AppExit 2 Restart
nssm set my-service-name AppDirectory "c:\path\to\my\app"

So, this lines will:

  1. Register a windows service named my-service-name who launches a copy of my jar (java) application.
  2. Set a parameter to NSSM to copy my-app.jar to snapshot.jar before start the service.
  3. Set a parameter to NSSM to specify that, when my app terminates the default behavior is assuming that the service must stop
  4. Set a parameter to NSSM to specify that, when my app terminates with the exit code 2 it must be restarted (my java application) and the service must continue to running.
  5. Set a parameter to NSSM to specify that my app will using the current directory as c:\path\to\my\app

Another solution is creating a batch file to be on loop, like this (I called it run-app.bat):

@echo off
set java=C:\Program Files (x86)\Java\jre1.8.0_192
:start
copy /y my-app.jar snapshot.jar
if %errorlevel% equ 0 goto :run
if %errorlevel% neq 0 goto :end
:run
"%java%\bin\java.exe" -jar snapshot.jar --start
if %errorlevel% equ 2 goto :start
:end
exit /b %errorlevel%

And using the NSSM to register the service in a simple way:

nssm install my-service-name "cmd /c run-app.bat"
nssm set my-service-name AppDirectory "c:\path\to\my\app"

In this scenario, the NSSM will just launch my batch run-app.bat.
The batch will stay on loop (restarting my app) while the application exits with code 2.

How to restart a program from a method

If you just want to make it work you can do something like this :

private static void game()//game method
{
boolean exit = false;
while(!exit){
//...int play = JOptionPane.showOptionDialog(null,"Play Again?", "Do you want to play again?", JOptionPane.PLAIN_MESSAGE,JOptionPane.DEFAULT_OPTION, null, again, again[1]);
//end of game

if (play == 0) {
exit = true;
}

}
System.exit(0);//exit

But a better more professionnal approach would be to refactor your code, so you extract game logic and separate it from User dialog interaction.

How to make your java application restarts itself

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

Restart java program

First, put (almost) everthing in the first section of the above code (until the do-while statement) in a introPhase() method or something.

Secondly, put your do-while in a second gamePhase() method or something.

Finally, surround both with a conditional loop statement of some sort.

You should end up with something like this:

boolean finishFlag = false;
while(!finishFlag) {
introPhase();
gamePhase();
finishFlag = getExitInfoFromUser();
}

Good luck!

Java Application Restart or Reset Button

Found the answer to my own question:

Thanks to Holger for suggesting "dispose". This is what I found worked.

private void restartButtonActionPerformed(ActionEvent evt)  {
if(evt.getSource() == restartButton)
{
dispose();
MyGUI game = new MyGUI();
game.setVisible(true);
}
}

How to restart java app managed by systemd on OutOfMemory errors

Like Fildor say I suggest you fix the memory problems.

After that a possible solution is:

If you are using Java prior 8u92 you can add to the JVM the following argument:

java -jar <jar-name> -XX:OnOutOfMemoryError="kill -9 %p"

in Java version 8u92 or higher you can use -XX:+CrashOnOutOfMemory or -XX:+ExitOnOutOfMemoryError

Then configure your service to restart on crash:

Restart=on-failure

or

Restart=always


Related Topics



Leave a reply



Submit