In this core java tutorial we will terminate JVM (Java virtual machine) using halt and exit method of java.lang.Runtime class in java.
Contents of page >
- 1) Terminate JVM (Java virtual machine) using halt method in java >
- About halt() method of java.lang.Runtime class in java >
- Example/program 1 to terminate JVM (Java virtual machine) using halt method in java >
- Example/program 2 to terminate JVM (Java virtual machine) using halt method And Show registered ShutdownHooks Are NOT Executed in java >
- 2) Terminate JVM (Java virtual machine) using exit method in java >
- About exit() method of java.lang.Runtime class in java >
- Example/program 3 to terminate JVM (Java virtual machine) using exit method in java >
- Example/program 4 to terminate JVM (Java virtual machine) using exit method And Show registered ShutdownHooks Are Executed in java >
finalize method in java - 13 salient features
1) Terminate JVM (Java virtual machine) using halt method in java >
About halt() method of java.lang.Runtime class in java >
- halt() method of Runtime class
- halt method terminates the running JVM (Java virtual machine) forcibly.
- halt method never returns normally in java.
- Two important points about halt method in java >
- 1) halt method does not allows even already registered ShutdownHook to be executed (shown in program 2 below) in java And
- 2) halt method even does not run the un invoked finalizers if finalization-on-exit has been enabled in java.
Example/program 1 to terminate JVM (Java virtual machine) using halt method in java >
package a2_terminateJvm;
/**
* Example/program to terminate
* JVM (Java virtual machine) using halt method
*/
public class TerminateJVMExampleUsingHaltMethod{
public static void main(String[] args) {
System.out.println("Halt method program - to terminate JVM (Java virtual machine)");
/**
* first we will get the java Runtime object using the
* Runtime class's getRuntime() method in java.
*/
Runtime runtime = Runtime.getRuntime();
/**
* halt method of Runtime class
* terminates the running JVM (Java virtual machine) forcibly.
* halt method never returns normally.
*
* Two important points about halt method in java >
* 1) halt method does not allows even already
* registered ShutdownHook to be executed (shown in program 2 below) And
* 2) halt method even does not run the un invoked finalizers
* if finalization-on-exit has been enabled.
*/
runtime.halt(1); //pass status as 1
System.out.println("JVM (Java virtual machine) halted"); //This line won't be printed
}
}
/* output
Halt method program - to terminate JVM (Java virtual machine)
*/
|
Example/program 2 to terminate JVM (Java virtual machine) using halt method And Show registered ShutdownHooks Are NOT Executed in java >
/**
* Example/program to terminate
* JVM (Java virtual machine)
* Using Halt Method And Show registered
* ShutdownHooks Are Not Executed
*/
public class TerminateJVMUsingHaltMethodAndShowShutdownHooksAreNotExecuted{
public static void main(String[] args) {
System.out.println("halt method program - to terminate JVM (Java virtual machine)");
Runtime runtime = Runtime.getRuntime();
//Register/ add ShutdownHook, it won't get called after
//JVM is shutDown using halt method.
Runtime.getRuntime().addShutdownHook(new Thread(){
public void run() {
System.out.println("Executing shutdown hook...");
System.out.println("shutdown hook executed successfully");
}
});
//call halt method
runtime.halt(1);
System.out.println("JVM (Java virtual machine) halted"); //This line won't be printed
}
}
/* output
halt method program - to terminate JVM (Java virtual machine)
*/
|
2) Terminate JVM (Java virtual machine) using exit method in java >
About exit() method of java.lang.Runtime class in java >
- exit method of Runtime class
- Exit method terminates the running JVM (Java virtual machine) by initiating the shutdown sequence in java.
- exit method never returns normally in java.
- Two important phases of shutdown sequence initiated by exit method in java >
- 1) exit method does allows to run already registered ShutdownHook to be executed (shown in program 4 below) And
- 2) halt method runs the uninvoked finalizers if finalization-on-exit has been enabled.
Example/program 3 to terminate JVM (Java virtual machine) using exit method in java >
/**
* Example/program to terminate
* JVM (Java virtual machine) using exit method
*/
public class TerminateJVMExampleUsingExitMethod{
public static void main(String[] args) {
System.out.println("Exit method program - to terminate JVM (Java virtual machine)");
/**
* first we will get the java Runtime object using the
* Runtime class's getRuntime() method in java.
*/
Runtime runtime = Runtime.getRuntime();
/**
* exit method of Runtime class
* terminates the running JVM (Java virtual machine) by
* initiating the shutdown sequence.
*
* exit method never returns normally.
*
* Two important phases of shutdown sequence initiated by exit method in java >
* 1) exit method does allows to run already
* registered ShutdownHook to be executed (shown in program 2 below) And
* 2) halt method runs the uninvoked finalizers
* if finalization-on-exit has been enabled.
*/
runtime.exit(1); //pass status as 1 to exit method
System.out.println("JVM (Java virtual machine) halted"); //This line won't be printed
}
}
/* output
Exit method program - to terminate JVM (Java virtual machine)
*/
|
Example/program 4 to terminate JVM (Java virtual machine) using exit method And Show registered ShutdownHooks Are Executed in java >
/**
* Example/program to terminate
* JVM (Java virtual machine)
* Using exit Method And Show registered
* ShutdownHooks Are Executed
*/
public class TerminateJVMUsingExitMethodAndShowShutdownHooksAreExecuted{
public static void main(String[] args) {
System.out.println("Exit method program - to terminate JVM (Java virtual machine)");
Runtime runtime = Runtime.getRuntime();
//Register/ add ShutdownHook, it will get called after
//JVM is shutDown using exit method.
Runtime.getRuntime().addShutdownHook(new Thread(){
public void run() {
try{
System.out.println("Executing shutdown hook...");
}catch (Exception e){
e.printStackTrace();
}
System.out.println("shutdown hook executed successfully");
}
});
//call exit method
runtime.exit(1);
System.out.println("JVM (Java virtual machine) exited"); //This line won't be printed
}
}
/* output
Exit method program - to terminate JVM (Java virtual machine)
Executing shutdown hook...
shutdown hook executed successfully
*/
|
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.
SUMMARY >
So in this core java tutorial we Terminated JVM (Java virtual machine) using halt and exit method in java with example and programs
We also learned program to terminate JVM using halt method And Show registered ShutdownHooks Are NOT Executed in java.
We also learned Example to terminate JVM using exit method And Show registered ShutdownHooks Are Executed in java.
RELATED LINKS>