JVM Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.10

Shutting Down or Terminating the JVM Instance

This section provides a tutorial example on how to shutdown or terminate the JVM instance using exit() or halt() method.

There are two ways to terminate the JVM instance from the running application:

  • Calling the exit() method, which initiates the JVM normal shutdown sequence: run all registered shutdown hooks; run all uninvoked finalizers; then end the JVM instance.
  • Calling the halt() method, which ends the JVM instance immediately.

Here is tutorial example program testing exit() and halt() methods:

/**
 * RuntimeShutdown.java
 * Copyright (c) 2010 by Dr. Herong Yang, herongyang.com
 */
class RuntimeShutdown {
   public static void main(String[] a) {
      java.io.PrintStream out = System.out;
      Runtime rt = Runtime.getRuntime();
      String opt = a[0];

      out.println("Adding a shutdown hook...");
      rt.addShutdownHook(new MyShutdownHook());

      if (opt.equals("exit")) {
         out.println("Asking JVM to shutdown...");
         rt.exit(0);
      } else if (opt.equals("halt")) {
         out.println("Asking JVM to terminate...");
         rt.halt(0);
      } else {
         out.println("Putting the application to sleep...");
         try {Thread.sleep(1000*60*60);} 
         catch (InterruptedException e) {}
      }
   }
   public static class MyShutdownHook extends Thread {
      public void run() {
         System.out.println("Running my shutdown hook...");
      }
   }
}

When executed on my Windows XP system with JDK 1.6.0, I got this result:

C:\herong\jvm>java RuntimeShutdown exit
Adding a shutdown hook...
Asking JVM to shutdown...
Running my shutdown hook...

C:\herong\jvm>java RuntimeShutdown halt
Adding a shutdown hook...
Asking JVM to terminate...

The test result confirms that:

  • My shutdown hook was executed during the JVM shutdown sequence initiated by the exit() method.
  • The JVM instance was terminated immediately when calling the halt() method. My shutdown hook was not executed.

Shutting down and terminating the JVM instance can also be initiated with Windows system tools like pressing Ctrl-C or using the Task Manager. To test these tools, you can run my test program with the third option:

C:\herong\jvm>java RuntimeShutdown sleep
Adding a shutdown hook...
Putting the application to sleep...
(press Ctrl-C on the console)
Running my shutdown hook...

C:\herong\jvm>java RuntimeShutdown sleep
Adding a shutdown hook...
Putting the application to sleep...
(terminate the Java process using Windows Task Manager)

As you can see from the test result:

  • Pressing Ctrl-C is equivalent to calling exit().
  • Terminate the Java process using Windows Task Manager is equivalent to calling halt().

Last update: 2010.

Table of Contents

 About This Book

 Download and Install Java SE 1.6 Update 2

java.lang.Runtime Class - The JVM Instance

 What Is Runtime?

 Printing Runtime Basic Information

 Running the Garbage Collector Explicitly

Shutting Down or Terminating the JVM Instance

 Executing System Commands

 Loading Native Libraries

 java.lang.System Class - The Operating System

 ClassLoader Class - Class Loaders

 Class Class - Class Reflections

 Sun's JVM - Java HotSpot VM

 JRockit JVM 7.0 by BEA Systems

 JRockit JVM 8.0 by BEA Systems

 Memory Management Rules and Tests

 Garbage Collection Tests

 Stack Overflow Tests

 Thread Testing Program and Result

 StringBuffer Testing Program and Result

 CDS (Class Data Sharing)

 Micro Benchmark Runner and JVM Options

 Micro Benchmark Tests on "int" Operations

 Micro Benchmark Tests on "long" Operations

 Micro Benchmark Tests in JIT Compilation Mode

 Micro Benchmark Tests on "float" and "double" Operations

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2010
Shutting Down or Terminating the JVM Instance