---
spec: JVMS
title: "The Java Virtual Machine Specification"
edition: "Java SE 25 Edition"
java_version: 25
chapter: "Chapter 5. Loading, Linking, and Initializing"
section: "5.7"
heading: "5.7. Java Virtual Machine Termination"
parent: "Chapter 5. Loading, Linking, and Initializing"
source: https://docs.oracle.com/javase/specs/jvms/se25/html/jvms-5.html#jvms-5.7
id: "5.7"
url: https://jvm.obfuscated.software/jvms/se25/5.7.html
raw: https://jvm.obfuscated.software/jvms/se25/5.7.md
---

# 5.7. Java Virtual Machine Termination

The Java Virtual Machine executes code in threads ([§2.5](2.5.md)). A thread is either a non-daemon thread, a daemon thread, or a shutdown hook.

Readers are referred to the API specifications of `Thread` and `Runtime` for details of how threads obtain daemon status, and how shutdown hooks are registered.

A thread *terminates* if either (i) its `run` method completes normally, or (ii) its `run` method completes abruptly and the relevant uncaught exception handler ([§2.10](2.10.md)) completes normally or abruptly. With no code left to run, the thread has completed execution and therefore has no current method ([§2.5.1](2.5.1.md)).

The Java Virtual Machine *terminates* when one of the following situations has occurred:

1. A thread invoked `System.exit` or `Runtime.exit`, and all of the shutdown hooks which consequently were started by the Java Virtual Machine, if any, have terminated.

2. A thread invoked `Runtime.halt`. (No shutdown hooks are started in this situation.)

3. The Java Virtual Machine implementation recognized an external event as requesting termination of the Java Virtual Machine, and all of the shutdown hooks which consequently were started by the Java Virtual Machine, if any, have terminated.

   The nature of the event is outside the scope of this specification, but is necessarily something that a Java Virtual Machine implementation can handle reliably. An example is receiving a signal from the operating system.

4. An external event occurred that the Java Virtual Machine implementation cannot handle. (No shutdown hooks are started in this situation.)

   The nature of the event is outside the scope of this specification, but is necessarily something that a Java Virtual Machine implementation cannot recognize or recover from in any way. Examples include a fatal error occurring in the process running the implementation, or power being removed from the computer running the implementation.

Upon Java Virtual Machine termination, any daemon or non-daemon thread that has not yet terminated will execute no further Java code. The current method of the thread does not complete normally or abruptly.

If the Java Virtual Machine terminates because a thread invoked `Runtime.halt` *while shutdown hooks were running*, then, in addition to daemon and non-daemon threads, any shutdown hook that has not yet terminated will execute no further Java code.

Native applications can use the JNI Invocation API to create and destroy the Java Virtual Machine in such a way that a Java program, having started execution in the `main` method of an initial class (JLS §12.1), exits when all of its non-daemon threads have terminated (JLS §12.8). The Java Virtual Machine does not terminate "automatically" when the last non-daemon thread terminates.
