---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 27 Edition"
java_version: 27
chapter: "Chapter 12. Execution"
section: "12.1"
heading: "12.1. Java Virtual Machine Startup"
parent: "Chapter 12. Execution"
source: https://docs.oracle.com/javase/specs/jls/se27/html/jls-12.html#jls-12.1
id: "12.1"
url: https://jvm.obfuscated.software/jls/se27/12.1.html
raw: https://jvm.obfuscated.software/jls/se27/12.1.md
---

# 12.1. Java Virtual Machine Startup

The Java Virtual Machine starts execution by invoking a `main` method of some specified class or interface. If this method has a formal parameter, it is passed a single argument which is an array of strings.

The precise semantics of Java Virtual Machine startup are given in Chapter 5 of *The Java Virtual Machine Specification, Java SE 27 Edition*. Here we present an overview of the process from the viewpoint of the Java programming language.

The manner in which the initial class or interface is specified to the Java Virtual Machine is beyond the scope of this specification, but it is typical, in host environments that use command lines, for the fully qualified name of the initial class or interface to be specified as a command line argument and for following command line arguments to be used as strings that are taken as the components of an array that is passed as an argument to the `main` method. If the original compilation unit was a compact compilation unit ([§7.3](7.3.md)), then the name of the file that contained the compilation unit, minus any extension, is typically used to specify the name of the initial class or interface.

For example, in a UNIX implementation, the command line:

```
java Test reboot Bob Dot Enzo
```

will typically start a Java Virtual Machine by invoking a `main` method of class `Test` (a class in an unnamed package), passing it an argument array containing the four strings "`reboot`", "`Bob`", "`Dot`", and "`Enzo`".

Whereas if the file `HelloWorld.java` contained the following compact compilation unit:

```
void main() {
    System.out.println("Hello, World!");
}
```

which has been compiled, then the command line:

```
java HelloWorld
```

will typically start a Java Virtual Machine by invoking the `main` method of the implicitly declared class ([§8.1.8](8.1.8.md)), producing the output:

```
Hello, World!
```

We now outline the steps the Java Virtual Machine may take to execute the initial class or interface; these are described further in later sections.

## 12.1.1. Load the Initial Class or Interface

The attempt to execute a `main` method of the initial class or interface discovers that it is not loaded - that is, that the Java Virtual Machine does not currently contain a binary representation for this class or interface. The Java Virtual Machine then uses a class loader to attempt to find such a binary representation. If this process fails, then an error is thrown. This loading process is described further in [§12.2](12.2.md).

## 12.1.2. Link the Initial Class or Interface: Verify, Prepare, (Optionally) Resolve

After the class or interface is loaded, it must be initialized before a `main` method can be invoked. The initial class, like all classes and interfaces, must be linked before it is initialized. Linking involves verification, preparation, and (optionally) resolution. Linking is described further in [§12.3](12.3.md).

Verification checks that the loaded representation of the initial class or interface is well-formed, with a proper symbol table. Verification also checks that the code that implements the initial class or interface obeys the semantic requirements of the Java programming language and the Java Virtual Machine. If a problem is detected during verification, then an error is thrown. Verification is described further in [§12.3.1](12.3.1.md).

Preparation involves allocation of static storage and any data structures that are used internally by the implementation of the Java Virtual Machine, such as method tables. Preparation is described further in [§12.3.2](12.3.2.md).

Resolution is the process of checking symbolic references from the initial class or interface to other classes and interfaces, by loading the other classes and interfaces that are mentioned and checking that the references are correct.

The resolution step is optional at the time of initial linkage. An implementation may resolve symbolic references from a class or interface that is being linked very early, even to the point of resolving all symbolic references from the classes and interfaces that are further referenced, recursively. (This resolution may result in errors from these further loading and linking steps.) This implementation choice represents one extreme and is similar to the kind of "static" linkage that has been done for many years in simple implementations of the C language. (In these implementations, a compiled program is typically represented as an "`a.out`" file that contains a fully-linked version of the program, including completely resolved links to library routines used by the program. Copies of these library routines are included in the "`a.out`" file.)

An implementation may instead choose to resolve a symbolic reference only when it is actively used; consistent use of this strategy for all symbolic references would represent the "laziest" form of resolution. In this case, if the initial class or interface had several symbolic references to another class, then the references might be resolved one at a time, as they are used, or perhaps not at all, if these references were never used during execution of the program.

The only requirement on when resolution is performed is that any errors detected during resolution must be thrown at a point in the program where some action is taken by the program that might, directly or indirectly, require linkage to the class or interface involved in the error. Using the "static" example implementation choice described above, loading and linkage errors could occur before the program is executed if they involved a class or interface mentioned in the initial class or interface or any of the further, recursively referenced, classes and interfaces. In a system that implemented the "laziest" resolution, these errors would be thrown only when an incorrect symbolic reference is actively used.

The resolution process is described further in [§12.3.3](12.3.3.md).

## 12.1.3. Initialize the Initial Class or Interface: Execute Initializers

At this stage, the Java Virtual Machine is still trying to execute a `main` method of the initial class or interface. This is permitted only if the class or interface has been initialized ([§12.4.1](12.4.1.md)).

Initialization consists of execution of any class variable initializers and static initializers of the initial class or interface, in textual order. But before it can be initialized, its direct superclass must be initialized, as well as the direct superclass of its direct superclass, and so on, recursively. In the simplest case, the initial class has `Object` as its implicit direct superclass; if class `Object` has not yet been initialized, then it must be initialized before the initial class or interface is initialized. Class `Object` has no superclass, so the recursion terminates here.

If the initial class has another class `Super` as its superclass, then `Super` must be initialized before the initial class. This requires loading, verifying, and preparing `Super` if this has not already been done and, depending on the implementation, may also involve resolving the symbolic references from `Super` and so on, recursively.

Initialization may thus cause loading, linking, and initialization errors, including such errors involving other classes and interfaces.

The initialization process is described further in [§12.4](12.4.md).

## 12.1.4. Invoke a `main` Method

Finally, after completion of the initialization for the initial class or interface (during which other consequential loading, linking, and initializing may have occurred), a `main` method declared in, or inherited by, the initial class or interface is invoked.

A `main` method declared in, or inherited by, a given class is a *candidate `main` method* if either:

- It has a single formal parameter ([§8.4.1](8.4.1.md)) whose declared type is an array of `String`, a `void` result, and `public`, `protected` or package access; or
- It has no formal parameters, a `void` result, and `public`, `protected` or package access.

Note that a candidate `main` method may be either a `static` or an instance method. It may optionally have a `throws` clause ([§8.4.6](8.4.6.md)). The type of the single formal parameter may be given as either `String[]` or `String...` as they denote the same type.

The form of a candidate `main` method expanded significantly in Java SE 25. Prior to that, it had to be `public`, `static`, and have a single formal parameter; the only variation possible was `String[]` versus `String...` for the type of the single formal parameter.

Note that it is *not* a compile-time error if the initial class or interface counts more than one candidate `main` methods among its members.

The presence of a candidate `main` method in a class or interface may not be immediately apparent because it may be inherited. For example, a default method in an interface is an instance method ([§9.4](9.4.md)), so may be a candidate `main` method when inherited by a class that implements the interface.

A candidate `main` method of the initial class or interface is invoked as if by application of the following rules:

- If there is a candidate `main` method with a single formal parameter, then this method is invoked. If this method is `static` then it is invoked directly, passing the argument array (§12.1). If the candidate `main` method is an instance method then first an instance of the initial class must be created. This class instance is the result of evaluating an unqualified class instance creation expression ([§15.9](15.9.md)) with no type arguments, no actual arguments, no class body, and where the *ClassOrInterfaceTypeToInstantiate* is the name of the initial class. The candidate `main` method is then invoked on the resulting instance, passing the argument array.
- Otherwise, if there is a candidate `main` method with no formal parameters, then this method is invoked. If this method is `static` then it is invoked directly. If the candidate `main` method is an instance method then first an instance of the initial class must be created. This class instance is the result of evaluating an unqualified class instance creation expression ([§15.9](15.9.md)) with no type arguments, no actual arguments, no class body, and where the *ClassOrInterfaceTypeToInstantiate* is the name of the initial class. The candidate `main` method is then invoked on the resulting instance with no actual arguments.

Note that it is not possible for a class to have both a `static` and an instance method with the same signature ([§8.4.2](8.4.2.md)).

If there is no candidate `main` method to invoke, or if creating an instance of the initial class when invoking an instance `main` method fails, then execution fails; the precise behavior of an implementation in this case is beyond the scope of this specification.
