Install Java and Write Your First Program
Install a modern JDK, verify the toolchain, and ship your first Java program from the command line without an IDE doing the magic for you.
What you'll learn
- ✓Pick a JDK distribution that fits your platform
- ✓Verify java and javac from a shell
- ✓Compile and run a HelloWorld program by hand
- ✓Understand what .class files and the JVM actually do
- ✓Use jshell for quick experiments
Prerequisites
- •A terminal you are comfortable typing in
Most Java tutorials start by clicking through an IDE wizard. That hides the toolchain you will eventually need to debug. We will skip the wizard, install a JDK, and compile a program from a shell so you know what every step does.
Pick a JDK
The JDK is the compiler plus the runtime. The JRE alone runs Java but does not compile it; you want the JDK. Pick an LTS version (21 is the current long term release). Good free distributions:
- Temurin (Eclipse Adoptium)
- Amazon Corretto
- Azul Zulu
On macOS with Homebrew:
brew install --cask temurin@21
On Ubuntu:
sudo apt update
sudo apt install openjdk-21-jdk
On Windows, download the Temurin MSI installer and let it set JAVA_HOME and PATH for you.
Verify the install
Open a fresh shell and check both binaries:
java --version
javac --version
Both should print 21.x. If javac is missing but java works, you installed the JRE; reinstall the JDK. If neither is found, your PATH does not include the JDK bin directory.
Set JAVA_HOME so build tools find the install:
# macOS / Linux
export JAVA_HOME="$(/usr/libexec/java_home -v 21)" # macOS
export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64 # Linux
Your first program
Create a file HelloWorld.java. The filename must match the public class name.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java 21");
}
}
Compile and run:
javac HelloWorld.java
java HelloWorld
javac produces HelloWorld.class, a bytecode file. java HelloWorld boots the JVM, loads that class, and calls main. Note you pass the class name to java, not the filename.
What just happened
The JVM is a virtual machine that runs bytecode. Bytecode is portable: the same .class file runs on Linux, Windows, and macOS as long as a JVM is installed. The JIT compiler inside the JVM translates hot bytecode into native machine code at runtime, which is why Java is fast despite being interpreted at first.
Three things are worth internalizing:
- Source files end in
.javaand are human readable. - The compiler emits
.classfiles (bytecode) into the current directory by default. - The runtime loads classes lazily as your program references them.
Single file source mode
Since Java 11 you can skip the compile step for quick scripts:
java HelloWorld.java
This compiles in memory and runs in one shot. Useful for snippets, not for projects.
jshell: a REPL for Java
Java ships with a REPL called jshell. Launch it:
jshell
Then try:
jshell> int x = 21
jshell> System.out.println(x * 2)
42
jshell> /exit
No class, no main, no semicolons required. Use it to test an expression before committing to a file.
Reading arguments
main receives a String[] of command line arguments. Try:
public class Greet {
public static void main(String[] args) {
String name = args.length > 0 ? args[0] : "stranger";
System.out.println("Hello, " + name);
}
}
javac Greet.java
java Greet Yash
# Hello, Yash
A note on packages
So far we used the default (unnamed) package. Real projects organize code into packages that mirror the folder structure. A file declared package com.codeloom.intro; must live in com/codeloom/intro/. When you graduate to a build tool like Maven or Gradle, this layout is required.
Where to go from the command line
You can keep using javac and java for small projects, but multi-file builds get painful fast. The standard next step is Maven or Gradle, which handle compilation, dependencies, and packaging into a JAR. For now, stay on the CLI long enough to be unafraid of it.
A useful next exercise: continue with Java Variables and Primitive Types and then Java Control Flow to build a foundation before touching classes.
Wrap up
You have a working JDK, you can compile and run Java without an IDE, and you understand the bytecode round trip. Keep the terminal open for the next few articles, and use jshell whenever you want to test a one liner without ceremony.