Skip to Content

Terminate virtual machine using System class

  1. /*
  2.         Terminate virtual machine using System class
  3.         This example shows how to get currently running virtual machine
  4.         by using exit method of Java System class.
  5. */
  6.  
  7. public class TerminateVM {
  8.  
  9.         public static void main(String[] args) {
  10.                
  11.                 /*
  12.                  * To terminate virtual machine use
  13.                  * public static void exit(int intStatus) method of
  14.                  * System class.
  15.                  *
  16.                  * Here, as per convention non zero status code should
  17.                  * be specified for any abnormal conditions.
  18.                  */
  19.                  
  20.                  System.out.println("Exiting current virtual machine");
  21.                  
  22.                  System.exit(0);
  23.                  
  24.                  System.out.println("This will not be printed");
  25.                  
  26.         }
  27. }
  28.  
  29. /*
  30. Typical output would be
  31. Exiting current virtual machine
  32. */