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:

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

/**
 * RuntimeShutdown.java
 * Copyright (c) 2010, HerongYang.com, All Rights Reserved.
 */
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 7 system with JDK 1.7.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:

Comparing to the same test with JDK 1.6.0 in 2010, the behavior of rt.exit() and rt.halt() is not changed.

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:

Last update: 2014.

Table of Contents

 About This Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

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 28.2.7 by Oracle Corporation

 JVM Runtime Data Areas

 Memory Management and Garbage Collectors

 Garbage Collection Tests

 JVM Stack, Frame and Stack Overflow

 Thread Testing Program and Result

 CPU Impact of Multi-Thread Applications

 I/O Impact of Multi-Thread Applications

 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

 Outdated Tutorials

 References

 PDF Printing Version