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) 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 with the latest JVM, I got this result:

herong> java RuntimeShutdown exit
Adding a shutdown hook...
Asking JVM to shutdown...
Running my shutdown hook...

herong> java RuntimeShutdown halt
Adding a shutdown hook...
Asking JVM to terminate...

The test result confirms that:

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

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

herong> 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:

Table of Contents

 About This Book

 JVM (Java Virtual Machine) Specification

 Java HotSpot VM - JVM by Oracle/Sun

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

 JVM Runtime Data Areas

 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

 OpenJ9 by Eclipse Foundation

 JRockit JVM 28.2.7 by Oracle Corporation

 Archived Tutorials

 References

 Full Version in PDF/EPUB