Java Runtime exit() Method with Examples
Last Updated :
23 Oct, 2023
Java Runtime exit() method is used to terminate currently running Java code in the Java virtual machine (JVM). This is an alternative way for System.exit(). Nevertheless, it is less commonly used than System.exit() because Runtime.getRuntime().exit() introduces an unnecessary object creation with a method or function calling. Likewise in System.exit() this method terminates the program and exits the JVM with the specified status code. In this article we are going to explore its syntax, and how we use it in our Java code with code demonstration.
Syntax:
Runtime.getRuntime().exit(0);
Runtime.getRuntime().exit(0): It indicates a successful/ normal termination, by convention.
Runtime.getRuntime().exit(1) or Runtime.getRuntime().exit(2) or any non-zero number: It indicates abnormal termination of the program. 1,2 can be used to distinguish between different types of errors.
Examples of Runtime exit()
We will discuss a few code snippets that demonstrate the usage of the Runtime exit method with different status codes.
Example 1:
Java
import java.io.*;
class GFG {
public static void main(String[] args) {
System.out.println( "This is a simple Java Code....." );
Runtime.getRuntime().exit( 0 );
System.out.println( "Successfully Executed............" );
}
}
|
Output
This is a simple Java Code.....
In the above code, we can see that the last print statement is not executed by the compiler. It happens due to this Runtime.getRuntime().exit(0); statement. This statement terminates the program and exits the JVM with the specified status code. We can see the status code here used is 0. Therefore it’s a successful or normal termination of the program.
Note : Function Runtime.getRuntime().exit(0); do not returns anything.
Example 2:
Java
import java.io.*;
class GFG{
public static void arraycall( int [] arr, int index){
try {
System.out.println( "Array at index " +index+ " is " +arr[index]);
}
catch (Exception e){
System.out.println( "Error has been occured : " +e );
Runtime.getRuntime().exit( 1 );
}
System.out.println( "Sucessfully executed....." );
}
public static void main(String[] args){
int [] arr = { 9 , 18 , 108 , 45 , 5 };
System.out.println( "Output 1 : " );
arraycall(arr, 2 );
System.out.println();
System.out.println( "Output 2 : " );
arraycall(arr, 10 );
}
}
|
Output
Output 1 :
Array at index 2 is 108
Sucessfully executed.....
Output 2 :
Error has been occured : java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 5
In this Java code, we pass an array and index to the method ‘arraycall’. This method will print the array with the given index as a function parameter. If the index is smaller than the length of the given array, it simply displays the output as the element at the given index. Otherwise, the code will throw an error, array index out of bound, and then terminate the currently running JVM.
In this code, we can see the status code used is 1,i.e. a nonzero value. This is because it is an abnormal termination of the program. It indicates that the program terminated with an error.
Conclusion
We can use it to terminate the Java Virtual Machine completely without completing any pending task.
- We can use it with a status code as 0 or any non-zero number.
- 0 shows that it’s a natural termination of code.
- Any nonzero number shows that it’s an abnormal termination of code, this means an error has occurred. We have seen both implementations of the status code with clear examples.
Alternatively, we can use System.exit(), since System.exit()
and Runtime.getRuntime().exit()
are functionally equivalent. The only difference is that Runtime.getRuntime().exit()
introduces an unnecessary object creation.
Share your thoughts in the comments
Please Login to comment...