Lesson 1 of 23
In progress
Install Java and run a program
Install a JDK, compile a class with javac, run it with java, and understand what the bytecode in between actually is.
Java has a reputation for ceremony. Some of it is deserved, and most of it disappears once you know what the two commands do. This lesson gets a program running and explains the pieces.
What you will learn
- Tell the JDK, the JRE and the JVM apart.
- Compile and run a class from the terminal.
- Recognise what a
.classfile contains.
Three names, one stack
- The JVM executes bytecode. It is the program that actually runs your code.
- The JRE is the JVM plus the standard library, enough to run a compiled program.
- The JDK is the JRE plus the compiler and tools, enough to build one.
You need the JDK. Install a current long-term-support release — Java 21 or later at the time of writing — from your package manager or from an OpenJDK distribution. Then check:
java -version
javac -version
Both should print the same major version. If java works and javac does not, you have a JRE.
The first program
public class Hello {
public static void main(String[] args) {
System.out.println("Hello from Java");
}
}
Save it as Hello.java. The file name must match the public class name exactly, including case. That rule is not decorative: the compiler and the runtime use the name to find the class.
javac Hello.java
java Hello
The first command writes Hello.class next to the source. The second runs it and prints the line.
Note that java Hello takes the class name, not the file name. java Hello.class is a common first error and produces a confusing message.
What the compiler produced
Hello.class contains bytecode: instructions for the JVM rather than for your processor. That is the trick behind "write once, run anywhere". The compiler does not know whether you will run on Windows, macOS or Linux; the JVM on each platform translates the same bytecode.
Modern JVMs compile hot bytecode to native machine code at runtime, which is why Java performance is close to native for long-running programs. The startup cost of that warm-up is real for short scripts, and irrelevant for a server.
You can read the bytecode with javap -c Hello, and the output is instructive once: getstatic, ldc, invokevirtual and so on. You will not write it by hand.
Anatomy of main
public static void main(String[] args) is the entry point, and every part matters:
publicso the JVM can call it from outside the class.staticso no object is needed.voidbecause nothing is returned to the operating system through it.String[] argsreceives command-line arguments.
Running java Hello one two gives args the values "one" and "two". Printing args.length is the smallest useful program that takes input.
Packages and folders
Real code lives in packages, which map to folders:
package com.example.greeter;
public class Greeter {
public static void main(String[] args) {
System.out.println("Packaged and running");
}
}
The file belongs at com/example/greeter/Greeter.java, and you run it with the fully qualified name:
javac com/example/greeter/Greeter.java
java com.example.greeter.Greeter
The package name is part of the class’s identity, so com.example.greeter.Greeter and Greeter are different classes even with identical contents. Most tools assume this layout, and fighting it costs more than following it.
Compiling many files
javac -d out $(find src -name "*.java")
java -cp out com.example.greeter.Greeter
-d out puts class files in a directory that mirrors the package structure. -cp out tells the runtime where to look for classes. That classpath concept follows you into every build tool; Maven and Gradle mainly generate it for you.
IDEs and the terminal
An IDE gives completion, refactoring and a debugger, and it hides the two commands above. Learn the commands first: when the IDE’s build output disagrees with reality, the terminal is how you find out why.
IntelliJ IDEA Community, VS Code with the Java extension pack and Eclipse are all fine. Pick one and resist switching for a month.
Naming the file hello.java while the class is Hello. On a case-insensitive filesystem the file opens fine, and javac still fails with "class Hello is public, should be declared in a file named Hello.java".
Write a class that prints its own name, the number of arguments it received, and each argument on its own line. Run it with two arguments.
Show solution
public class Args { public static void main(String[] args) { System.out.println("Args"); System.out.println("count: " + args.length); for (String arg : args) { System.out.println(arg); } } } javac Args.java then java Args one two prints the count 2 and both values. The enhanced for loop reads each element without an index, which is the form to prefer when the index is not needed.
Recap
- The JDK contains the compiler; the JRE runs programs; the JVM executes bytecode.
javaccompiles to.class,javaruns a class by name.- Packages map to folders, and the classpath tells the runtime where the compiled classes live.
Next, you will meet the class itself: fields, methods and the object that holds them.
Which Java version should I install?
Why does java Hello.class fail?
Progress is saved in this browser only. No account, no email.