Open In App

Java Runtime exit() Method with Examples

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:




import java.io.*;
  
class GFG {
    public static void main(String[] args) {
         
       //Printing intial statement
       System.out.println("This is a simple Java Code....."); 
         
       //executing the mentioned function
       Runtime.getRuntime().exit(0);
         
       //Printing the final block
       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:




import java.io.*;
  
class GFG{
    public static void arraycall(int[] arr,int index){
          
        try{
            //Printing the array at given index if index is smaller than length .          
            System.out.println("Array at index "+index+" is "+arr[index]);
        }
        catch (Exception e){
              //Printing the Error
            System.out.println("Error has been occured : "+e );
              
            //executing the mentioned function
            Runtime.getRuntime().exit(1);
        }
          
        //Printing the final statment. It will be executed when catch block is not executed      
        System.out.println("Sucessfully executed.....");
    }
    //Main Function
    public static void main(String[] args){
          
        int[] arr = {9, 18, 108, 45, 5}; 
        //Output 1
        System.out.println("Output 1 : ");
        arraycall(arr,2);
          
          //Output 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.

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.


Article Tags :