Week 33 — What would be a possible way to restart a java application?
Question of the Week #33
What would be a possible way to restart a java application?
5 Replies
If you are running from jar
Windows:
1. Go to Task Manager.
2. Find java and click "End Task".
3. Double click on the jar.
Linux:
1. .
2. .
If you are running from IDE (IntelliJ IDEA, Eclipse, e.t.c.)
1. Click "Debug" or "Run" while the project is running.
2. A notification saying "Are you sure you want to restart?" will appear, Click the positive answer.
if it is not a Java Swing project with close operation set to nothing, just click X on the top panel of the window
Submission from geuxy#0000
Spring boot provides the actuator enpoint that can be called to do shutdown POST .../actuator/shutdown (you need to configure it to true as per default this is disabled. For vanilla Java System.exit(0) - when you want to go though the shutdown hooks. or Runtime.getRuntime().halt(1); when you want to skip said hooks.
Submission from cazacu23#0000
Restarting an application after it exits, errors or does other things usually involves the application host restarting it itself, so using something like a shell script to wrap the java command would most likely be used:
Java itself has a feature to run code when the jvm shuts down, called shutdown hooks. You can register one yourself using
Runtime#addShutdownHook(Thread)
, but using hooks to restart the application would be a misuse, especially since the jvm fully expects to shut down after all threads have executed.
Another way to do it directly in java would be to rewrite the main method like so: , although this wouldn't have some of the benefits the shell script version has. If the jvm itself breaks somehow, this won't restart it entirely, it'll just restart the main method. This also wouldn't work if System.exit() would be called, since that just ends the vm (almost) instantly. Shutdown hooks are still executed tho.⭐ Submission from 0x150#0000
Java does not natively support restarting the current application.
However, there are many ways to restart it.
The simplest way to do that would be to create a script that automatically restarts the Java application once it is stopped. This can also include a check for the return value so that the application only restarts on specific return values (e.g.
0
):
The application can then restart using System.exit(0);
and stop using System.exit(1);
(or any other non-zero exit code).
Alternatively, Java applications can just run themselves before exiting:
However, this approach will not inherit the console (System.in
/System.out
) as the restarted application will run in the background and requires the application to know where it is located and assumes that the correct java
executable is on the PATH
.
The latter issue can be prevented by dynamically discovering the location of the JAR file:
Note that this approach does not include the classpath/modulepath when restarting the application.
In many cases, restarting the application is actually not required. but it is sufficient to reset all public state, stop everything happening in the background and run the main
method again. Some libraries/frameworks provide functionality for stopping background activities of the said library/framework.
A fundamentally different approach is using a ClassLoader
.
This works by splitting the application in two: One JAR that contains the application code and one program (can also be a JAR) that is responsible for loading and restarting the other program.
The loading program loads the JAR with the application code and runs its main method using a URLClassLoader
.
It also needs to provide a public
method that can be invoked from the application code in order to restart it.
From the application code, the program can be restarted using
This calls the
restart
method of the loader which first closes the loader which loaded the application code and loads it again.
This allows restarting the application code and applying possibly changes in the JAR due to an update as well as clearing all static
variables in the application code as these are initialized when the classes are loaded.
However, the application code must make sure that all application code (threads started by it) are stopped when the application is restarted.
It is important that the loader (the Restarter
class) is not available inside the JAR. Both should be distinct programs. It is possible to compile the application code with Restarter
in the classpath but Restarter
must not be in the JAR of the application code.
This approach keeps System.in
/System.out
/System.err
as the restarted application runs in the same process but automatically clears all state (static variables). However, the application code must stop by itself without calling System.exit
.⭐ Submission from dan1st#0000