Exit Methods Should Not Be Called in Java
What is it?
This practice warns against using exit methods such as System.exit(int status)
, Runtime.getRuntime().exit(int status)
, and Runtime.getRuntime().halt(int)
, which terminate the Java Virtual Machine (JVM) abruptly.
Why apply it?
These exit methods can unexpectedly shut down an entire application, disrupting services and causing data loss. They should be avoided, especially in managed environments like J2EE containers, where you don't have full control over the system. It is crucial to manage application lifecycles gracefully without forcibly terminating them.
How to fix it?
Wherever possible, refactor the code to handle application shutdowns gracefully using standard application lifecycle handling techniques. This approach avoids an abrupt shutdown and provides resources with an opportunity to clean up.
Examples
Example 1:
Negative
A negative example demonstrating the use of System.exit()
, which immediately ends the JVM.
public class App {
public void run() {
// Application logic here
if (checkShutDownCondition()) {
System.exit(0); // Noncompliant
}
}
private boolean checkShutDownCondition() {
// Logic to determine if the application should shut down
return true;
}
}
Example 2:
Positive
A positive example that gracefully handles shutdown using a flag and appropriate application logic, without using exit methods.
public class App {
private boolean isRunning = true;
public void run() {
while (isRunning) {
// Application logic here
if (checkShutDownCondition()) {
shutdown();
}
}
}
private boolean checkShutDownCondition() {
// Logic to determine if the application should shut down
return false;
}
private void shutdown() {
isRunning = false;
// Additional cleanup logic here
System.out.println("Application shutdown gracefully.");
}
}
Negative
A negative example showing the use of Runtime.getRuntime().halt()
, which immediately terminates the JVM without cleanup.
public class Service {
public void startService() {
// Service operations
if (shouldStopService()) {
Runtime.getRuntime().halt(0); // Noncompliant
}
}
private boolean shouldStopService() {
// Determine when to stop the service
return true;
}
}
Example 3:
Positive
A positive example shows a managed shutdown without using any JVM termination methods.
public class Service {
private volatile boolean active = true;
public void startService() {
while (active) {
// Service operations
if (shouldStopService()) {
stopService();
}
}
}
private boolean shouldStopService() {
// Determine when to stop the service
return false;
}
public void stopService() {
active = false;
// Resource cleanup
System.out.println("Service stopped gracefully.");
}
}