How Java runs: JDK, JRE and JVM
Java code goes through two steps. First javac compiles your .java file into bytecode (.class files). Then the JVM (Java Virtual Machine) runs that bytecode on any operating system. This is the famous "write once, run anywhere" idea.
- JVM runs bytecode and manages memory and garbage collection.
- JRE is the JVM plus the core libraries needed to run programs.
- JDK is the JRE plus developer tools:
javac,java,jshell,jar,javadocand a debugger.
As a learner, always install a JDK, ideally a current long-term support release such as 25. Since Java 11 there is no separate JRE download from Oracle; you can build slim runtimes with jlink instead.
Since Java 25 your first program can be a single void main() method: no class declaration, no static, no String[] args.
JDK, JRE and JVM
- The JVM (Java Virtual Machine) runs bytecode on your operating system.
- The JRE is the JVM plus the standard library. Since Java 11 there's no separate JRE download; you build a small runtime with
jlinkif you need one. - The JDK is everything you need to develop: the runtime plus tools such as
javac,java,jshell,jar,javadoc,jdepsandjlink.
Install a JDK (for example Eclipse Temurin or Oracle JDK) and check it with java -version.
Compiling and running a program
javac turns .java source files into .class files containing bytecode, and java runs them. Since Java 11 you can run a single source file directly, and since Java 22 the launcher can run a program made of several source files.
javac Hello.java # creates Hello.class
java Hello # runs it
java Hello.java # Java 11+: compile in memory and runThe main method (and the simple main of Java 25)
A classic program starts at public static void main(String[] args). Java 25 finalised compact source files: a file can hold just a void main() method, with IO.println for output, which is ideal for learning and small scripts.
// Classic
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, Java");
}
}
// Java 25+: a complete program in Hello.java
void main() {
IO.println("Hello, Java");
}Command-line arguments
Words after the class name arrive in the args array as strings. Convert them yourself, and check the length before reading them.
public class Greet {
public static void main(String[] args) {
if (args.length < 2) {
System.out.println("Usage: java Greet <name> <times>");
return;
}
int times = Integer.parseInt(args[1]);
for (int i = 0; i < times; i++) System.out.println("Hi " + args[0]);
}
}
// java Greet Asha 3Bytecode and "write once, run anywhere"
Bytecode is the same on Windows, Linux and macOS; each platform's JVM runs it. The JVM interprets it at first and the JIT compiler turns hot code into native machine code. You can see the bytecode of a class with javap -c.
JShell: try code instantly
jshell (Java 9) is an interactive shell: type an expression and see the result, with no class or main method. It's perfect for checking how an API behaves.
$ jshell
jshell> "java".toUpperCase()
$1 ==> "JAVA"
jshell> List.of(3, 1, 2).stream().sorted().toList()
$2 ==> [1, 2, 3]JAR files and the classpath
A JAR is a zip of compiled classes and resources. The classpath tells the JVM where to find classes. An executable JAR names its main class in the manifest, so it runs with java -jar. Spring Boot builds such "fat" JARs for you.
jar --create --file app.jar --main-class com.shop.App -C out .
java -jar app.jar
java -cp "app.jar:libs/*" com.shop.App # use ; instead of : on WindowsExample
# Compile, then run (every version)
javac Hello.java
java Hello
# Run a source file directly (Java 11+)
java Hello.java
# Try snippets interactively (Java 9+)
jshellOld way vs new way
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}void main() {
IO.println("Hello, Java!");
}Common mistake
Installing an old Java 8 because a tutorial said so, or forgetting to set JAVA_HOME. Install a current LTS JDK and check with java -version and javac -version.
Under the hood
The JVM loads classes lazily through a chain of class loaders (bootstrap, platform, application). Bytecode is first interpreted, then hot methods are compiled to native code by the JIT compilers: C1 for fast startup, C2 for peak speed. That's why Java services "warm up" after starting. Project Leyden's ahead-of-time caches (Java 24 to 26) record that warm-up work so later runs start faster.
Check yourself
Which tool turns Hello.java into Hello.class?
How this connects
Know these first
Nothing. This is a good place to start.
Part of Java from zero.
Was this lesson helpful?
Finished reading? Mark it complete to track your progress.