Hello World (first programs) · Beginner · 14 min
Hello World in Java: compile with javac and run on the JVM
By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · Eclipse Temurin JDK 25.0.5
You will install the Eclipse Temurin JDK 25, compile and run a classic Java Hello World, run the shorter Java 25 form straight from source, and package a greeting program as a runnable JAR.
On this page
Java does not turn your code straight into instructions for your processor. The compiler, javac, turns each class into a .class file of bytecode, a compact instruction set for the Java Virtual Machine (JVM). The java command then starts a JVM, which loads that bytecode and runs it, compiling the busy parts to machine code while the program runs. The same .class file runs unchanged on Linux, Windows and macOS, on an Intel laptop or an ARM server. That is the idea behind Java's old promise, "write once, run anywhere".
In this tutorial you install a JDK, write the classic Hello World and watch it become bytecode, then use the shorter style that Java 25 made final. One step further, you read a name from the command line and package the program as a JAR file that anyone with Java 25 can run.
You will use JDK 25 from Eclipse Temurin, a free, open-source build of OpenJDK. JDK 25 is the current long-term support (LTS) release, which means it gets security and bug-fix updates for years. JDK 27 is newer: it came out in September 2026 and is a feature release with only about six months of updates before JDK 28 replaces it. Learn on the LTS. Most companies, courses and libraries target it, and everything here also works on 27.
Step 1 · Install the Temurin JDK
A JDK (Java Development Kit) contains everything for writing Java: the compiler javac, the java launcher, the jar packaging tool and the class library. A JRE (Java Runtime Environment) is the smaller set needed only to run programs that are already compiled, with no compiler. To write Java you need the JDK. Temurin still publishes separate JRE builds for servers that only run programs.
On Ubuntu 26.04, add the Adoptium apt repository so that apt upgrade brings you Java security updates. First install the tools to fetch and check its signing key, then add the key and the repository:
sudo apt update
sudo apt install -y curl gpg ca-certificates
sudo install -d -m 0755 /etc/apt/keyrings
curl -fsSL https://packages.adoptium.net/artifactory/api/gpg/key/public | sudo gpg --dearmor --yes -o /etc/apt/keyrings/adoptium.gpg
echo "deb [signed-by=/etc/apt/keyrings/adoptium.gpg] https://packages.adoptium.net/artifactory/deb $(. /etc/os-release && echo "$VERSION_CODENAME") main" | sudo tee /etc/apt/sources.list.d/adoptium.listThe curl line downloads Adoptium's public key and gpg --dearmor stores it in the binary form apt reads. The echo line writes one repository line. $(. /etc/os-release && echo "$VERSION_CODENAME") fills in your Ubuntu release name, resolute on 26.04, and signed-by tells apt to trust this key for this repository only. Now install the JDK:
sudo apt update
sudo apt install -y temurin-25-jdk
java -version
javac -versionOn Windows or macOS, open the Temurin releases page at adoptium.net, choose version 25 (LTS), your operating system and processor, and download the .msi (Windows) or .pkg (macOS) installer for the JDK. On Windows, turn on the installer options that add Java to PATH and set JAVA_HOME. Then open a new terminal and run java -version and javac -version.
Check it worked
java -version prints three lines that start with something like openjdk version "25.0.5" and mention Temurin. javac -version prints javac 25.0.5 or a later 25 patch. If java works but javac is not found, you installed a JRE instead of the JDK.
Step 2 · Write the classic Hello World
Make a throwaway folder for this tutorial and create Hello.java in it:
mkdir -p ~/hello-java-demo
cd ~/hello-java-demo
cat > Hello.java <<'JAVA'
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
JAVAYou can also paste the five lines into your editor and save the file as Hello.java in that folder. Every line has a job:
public class Hello {declares a class calledHello. In Java, all code lives inside a class.publicmeans code anywhere can use it, and the file name must match a public class's name exactly, including the capital H:Hellogoes inHello.java.public static void main(String[] args) {declares the method thejavalauncher looks for to start a program.staticmeans it belongs to the class, so the JVM can call it without creating aHelloobject first.voidmeans it returns nothing.String[] argsis an array of the words typed after the program name on the command line.System.out.println("Hello, world!");prints the text and a line break.Systemis a class from the standard library,outis its standard output stream andprintlnmeans "print line". Every statement ends with a semicolon.- The two
}close the method and then the class. Indentation is for people; Java reads the braces.
Check it worked
cat Hello.java shows the five lines, and the file is called Hello.java with a capital H.
Step 3 · Compile to bytecode and run it on the JVM
Compile the source file, then list the folder:
javac Hello.java
lsjavac prints nothing when it succeeds. It writes Hello.class, named after the class rather than the source file. Now ask the JVM to run the class. Give java the class name, not the file name, so there is no .class on the end:
java HelloTo see what the JVM actually runs, disassemble the bytecode with javap, which ships with the JDK:
javap -c HelloCheck it worked
ls shows Hello.class next to Hello.java, and java Hello prints Hello, world!. In the javap output, the main method contains lines something like getstatic for System.out, ldc for the "Hello, world!" string and invokevirtual for println. Those are JVM instructions, not processor instructions, which is why you can copy Hello.class to another computer with Java 25 and run it there without compiling again.
Step 4 · Write the Java 25 compact form
The classic version asks a beginner to type public, static, a class and an array before printing anything. JDK 25 made a shorter form final, called a compact source file. Put it in its own folder:
mkdir -p ~/hello-java-demo/compact
cd ~/hello-java-demo/compact
cat > Hello.java <<'JAVA'
void main() {
IO.println("Hello from compact Java!");
}
JAVA
java Hello.javaThree things changed:
- There is no class declaration. When a file has methods outside any class, the compiler wraps them in a class for you, named after the file.
void main()needs nopublic, nostaticand noargs. In JDK 25 the launcher accepts this simpler main method and creates an object to call it on.IO.printlncomes fromjava.lang.IO, a small class added for printing and reading lines. You can write it without animport, likeString.
The command java Hello.java is different from java Hello too. Given a .java file, the launcher compiles it in memory and runs it straight away, leaving no .class file behind. It is quick for small programs and scripts. Bigger projects still compile with javac or a build tool such as Maven or Gradle.
The separate folder matters. If java Hello.java runs in a folder that already holds a compiled Hello.class, the launcher can stop with an error saying the class was found on the application class path, because two versions of Hello would clash.
Check it worked
java Hello.java prints Hello from compact Java!, and ls in the compact folder shows only Hello.java, with no .class file.
Step 5 · Greet by name with command-line arguments
Make the program greet whoever you name on the command line. Create Greet.java in the same compact folder:
cat > Greet.java <<'JAVA'
void main(String[] args) {
String name = args.length > 0 ? args[0] : "world";
IO.println("Hello, " + name + "!");
IO.println("Your name has " + name.length() + " characters.");
}
JAVA
java Greet.java
java Greet.java AmaraLine by line:
void main(String[] args)asks for the arguments again. The compact form lets you add them back when you need them.String name = ...declares a variable that can only hold text. Java checks types when it compiles, so putting a number innameis a compile error, not a surprise at run time.args.length > 0 ? args[0] : "world"reads "if there is at least one argument, use the first one, otherwise useworld". Arrays count from 0, soargs[0]is the first word after the file name.+joins strings, andname.length()counts the characters in the name.
A name such as Chiamaka or Oluwaseun counts as you would expect. Names with accents, such as Adéolá, can count more than the letters you see when the accent is stored as a separate character. Java counts UTF-16 code units, which matters once you handle text in many languages.
Check it worked
The first run prints Hello, world! and Your name has 5 characters. The second prints Hello, Amara! and Your name has 5 characters.
Step 6 · Package a runnable JAR
A JAR (Java ARchive) is a zip file of .class files plus a manifest that can name the class to start. It is how Java programs are usually handed to other people. Compile Greet.java to a class file, then package it:
javac Greet.java
jar --create --file greet.jar --main-class Greet Greet.class
java -jar greet.jar Kofijavac Greet.java writes Greet.class, named after the file because the compiler made the class for you. In the jar command, --create --file greet.jar makes a new archive, --main-class Greet writes the start class into the manifest, and Greet.class is the file to include. java -jar reads the manifest and runs that class's main.
A JAR like this is a few kilobytes, small enough to send over chat on a prepaid data plan, but the person running it needs Java 25 or later installed. Bytecode compiled by JDK 25 does not run on an older JVM.
Check it worked
java -jar greet.jar Kofi prints Hello, Kofi! and Your name has 4 characters. Run jar --list --file greet.jar and you see META-INF/MANIFEST.MF and Greet.class.
Step 7 · Fix the mistakes beginners hit
You will see these sooner or later. Each one has a fixed cause:
class Hello is public, should be declared in a file named Hello.java: the public class name and the file name differ, often only by case (hello.java). Rename the file or the class so they match exactly.Could not find or load main class Hello.class: you typedjava Hello.class. Givejavathe class name,java Hello, or the source file,java Hello.java.javac: command not found: you have a JRE or no Java at all on yourPATH. Install the JDK, and on Windows open a new terminal after the installer finishes.UnsupportedClassVersionErrormentioning class file version 69.0: the class was compiled with JDK 25 and you ran it on an older Java. Runjava -version; on Ubuntu,sudo update-alternatives --config javachooses between installed versions.';' expected: a statement is missing its semicolon. The compiler points at the line, but the missing;is often at the end of the line before.
Before you clean up, make one error on purpose. Go back to the parent folder, where Hello.java is the classic program with public class Hello, copy it to a wrongly named file and compile that:
cd ~/hello-java-demo
cp Hello.java Greeting.java
javac Greeting.java || trueThe cd matters: in the compact folder, Hello.java has no public class, so the copy would compile without complaint. javac fails here, as intended, and || true only stops that expected failure from counting as an error if you paste these lines into a script. Read the error, then delete the bad copy:
rm Greeting.javaClean up when you are done. This leaves the folder first, so your terminal is not sitting in a directory that no longer exists, then removes only the folder you made:
cd ~
rm -rf ~/hello-java-demoKeep the JDK if you want to carry on with Java or the other JVM languages. To remove it, run sudo apt remove temurin-25-jdk.
Check it worked
Compiling Greeting.java printed the first error in the list above, naming Hello.java as the file the class belongs in. After the cleanup, ls ~/hello-java-demo reports that there is no such file or directory.
Where Java is used
Java runs a large share of the world's server software: bank and payment systems, insurance and telecom back ends, and online shops, often on frameworks such as Spring Boot. Android apps were written in Java for years and the Android platform still uses its libraries, although Kotlin is now Google's preferred language there. Big data tools such as Apache Kafka, Hadoop and Spark run on the JVM, and so does Minecraft Java Edition. Because Kotlin, Scala and Groovy compile to the same bytecode, what you learnt about javac, class files and JARs carries over to them.
Next steps
- Write the same program in Kotlin, the JVM language Android prefers: Hello World in Kotlin.
- Try a JVM language that mixes objects and functional programming: Hello World in Scala.
- See a dynamic JVM language used for build scripts: Hello World in Groovy.
Related tutorials
- Hello World in Zig: zig run, zig build and zig ccYou will install a signature-checked Zig 0.17.1 on Ubuntu 26.04, write and run Hello World with the current I/O API, build a small project with zig build that handles errors explicitly, and use zig cc to compile a C program for Linux and for Windows.
- Hello World in x86-64 assembly with NASM and Linux system callsYou will write an x86-64 assembly program that prints a line using only Linux system calls, assemble it with NASM, link it with ld, check its exit status, then loop it three times and read its machine code with objdump.
- Hello World in WebAssembly text format with wabt and WasmtimeYou will write a WebAssembly module by hand in the text format, assemble it with wat2wasm, print Hello World through WASI in Wasmtime 50, and call an exported add function from both Wasmtime and Node.js.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials