Ocp Executing The Jvm
OCP - Executing the JVM
When a Java program is “executed” or “ran” what is actually happening is that we are executing the Java Virtual Machine (JVM) and passing the Fully Qualified Class Name (FQCN), of a class containing a main
method, to the JVM as an argument.
The FQCN that is passed to the JVM must contain a main
method in a very specific format otherwise the JVM will generate an error and the program will not execute.
The main method
The main
method:
- Must be called
main
- Must return
void
- Must be
static
- Must contain exactly one parameter
- The only parameter must be a
String[]
or aString...
(varargs
) - Can optionally declare any exception in its
throws
clause
An example of valid main
method:
public static void main(String[] args) {
}
Examples of invalid main
methods include:
static void main(String args[]) {
// The method is not public
// The array is valid
}
public void main(String args[]) {
// The method is not static
}
public static void main(String args[], String additional) {
// The method has more than one parameter
}
Command line arguments
As well as passing the FQCN to the JVM you can optionally pass arguments. These arguments must follow the FQCN and must be separated by spaces, for example: java MainClass arg1 arg2
.
These args are passed to the main method in the String[]
parameter in the order that they are declared. You can use ""
to pass parameters that have spaces within them, for example: java MainClass "This is one value"
. Everything inside the quotes will be treated as a single String
object and will be accessible by the first index in the String[]
.
Completing the main method
The main
method is the entry point into the program. An application does not end just because all of the code inside the main
method has been executed. It is likely that the main method has invoked methods on other objects, and created other threads which must also complete execution.
The program only ends when all threads have completed executing.
Compile and run a Java program
The standard Oracle Java Development Kit (JDK) comes with a Java compiler. This compiler can be executed using javac
. For example: javac MainClass.java
. This will result in one or more Java classes being created, one of which must contain a main
method.
Note that we must specify the .java
suffix when compiling source files.
The standard JDK also comes with a JVM. The JVM can be executed using java
. For example: java MainClass
.
Note that we do not specify the .class
suffix when executing the JVM with the name of the class containing the main
method.
Run a source file in Java 11
Since Java 11 it is possible to execute the JVM with a single source file only, without having to first compile the source file into a class file. For example: java MainClass.java
.
Executing the JVM with a JAR
A Java program could contain many hundreds or thousands of class files, and it would be difficult to specify all of them when executing the JVM, or when sharing the program with other people.
For this reason Java has the Java Archive (JAR)
file format. A JAR file can be created which will contain all class files within a directory structure based on the specified package names.
For the following file structure:
c:
-- program
-- MainClass.java
Where MainClass.java
has defined the package package com.example
.
Running the command: jar -cvf program.jar .
will produce:
c:
-- program
-- MainClass.java
-- program.jar
The program.jar
file will contain the following file structure:
-- com
-- -- example
-- -- -- MainClass.class
To execute the JVM with this jar
file use the command java -classpath .\program.jar com.example.MainClass
. The -classpath
is telling the JVM where to look for class files. This is then followed by specifying the FQCN of the class containing the main
method.
Using a manifest for execution
Instead of telling the JVM explicitly where to look for the class files and which class contains the main
method. We can instead create a MANIFEST.MF
file. For example:
c:
-- program
-- MainClass.java
-- META-INF
-- -- MANIFEST.MF
The contents of the MANIFEST.MF
should atleast contain the FQCN of the class containing the main
method, for example:
Manifest-Version: 1.0
Main-Class: com.example.MainClass
Created-By: Chris Mepham
After creating the jar
file now, it will be runnable, and the JVM can be executed with java -jar program.jar
.
Alternatively, if you have already created the jar
file, you can create the MANIFEST.MF
file from the command line via the command jar -cvfm program.jar manifest.text com.example.MainClass
.
c
: createv
: verbosef
: output filem
: create a manifest file with the specified class containing themain
method