Run your first Java program
›Short versionOpen if you only have a minute.
- Install a JDK, then open a fresh terminal and confirm that both java and javac answer.
- PATH is the list of folders the shell searches. The JDK bin folder has to be on that list.
- Save public class HelloWorld in a file named HelloWorld.java, capitals included.
- javac HelloWorld.java writes HelloWorld.class beside the source.
- java HelloWorld runs that class. The command uses the class name, without .java or .class.
By the end of this page you have installed Java, saved a tiny file, and watched it print a line. The proof is the words Hello, World! in a .
You need a , any text editor you already trust, and that terminal. An IDE can wait until the file is larger than one class.
The JDK installer finished, and a new terminal still says java is not found. What is the usual cause?
The shell searches the folders on PATH. The JDK bin folder is missing from that list, so a finished install still answers as if Java were absent. Open a new terminal after the path change.
Install the kit
The JDK is the same language whether you take Oracle's build or OpenJDK. Oracle's JDK is the official download and includes a commercial support option. OpenJDK is the free, community build, and it is the one to use while you are learning. The Linux commands below install OpenJDK 17. The Windows and macOS installers come from the same Oracle downloads page, and the folder name follows the version you pick.
1. Windows. Open Oracle JDK Downloads and take the Windows x64 Installer, the .exe. Run it and click Next through the wizard. When it finishes, note the folder it chose. That is usually C:\Program Files\Java\jdk-XX, with XX standing in for the version on the installer.
2. macOS. From the same Oracle JDK Downloads page, take the macOS Installer, the .dmg, open it, and follow the wizard. If Homebrew is already on the machine, this installs OpenJDK instead:
brew install openjdk
3. Linux. Ask the package manager for OpenJDK 17. On Ubuntu or Debian:
sudo apt update && sudo apt install openjdk-17-jdk
On Fedora:
sudo dnf install java-17-openjdk-devel
Tell the shell where Java lives
The installer puts the tools in a folder. The shell still needs that folder on its . JAVA_HOME is a variable that stores the JDK directory, so PATH can point at bin inside it and you can type java from any directory.
1. Windows. Press Win + X, then open System, Advanced system settings, and Environment Variables.
2. Create JAVA_HOME. Under System variables, click New. The name is JAVA_HOME. The value for a JDK 17 install is C:\Program Files\Java\jdk-17. If the installer used a different jdk-XX folder, use that folder.
3. Update Path. Find Path in System variables, click Edit, then New, and add %JAVA_HOME%\bin.
4. Close the dialogs. Click OK on each window, then open a new terminal. Windows sometimes keeps the old Path until you restart the computer.
macOS and Linux. Add these lines to your shell file, ~/.bashrc, ~/.zshrc, or ~/.bash_profile. The path below matches the OpenJDK 17 package layout used above. If the real folder name differs, point JAVA_HOME at the folder that exists on disk.
export JAVA_HOME=/usr/lib/jvm/java-17-openjdkexport PATH=$JAVA_HOME/bin:$PATH
Load the file you edited. For bash that is source ~/.bashrc.
Check that the commands answer
Open a new terminal and ask for the runtime version. A JDK 17 install answers with a block in this shape. The sample below is 17.0.1, the build this page records. Your update number follows the installer you ran.
java -version
Sample output
java version "17.0.1" 2021-10-19 LTSJava(TM) SE Runtime Environment (build 17.0.1+12-LTS-39)Java HotSpot(TM) 64-Bit Server VM (build 17.0.1+12-LTS-39, mixed mode)
Then ask the compiler. should report the same generation as java.
javac -version
Sample output
javac 17.0.1
When the shell cannot find the command
A reply of command not found or not recognized means PATH still misses the JDK bin folder. Recheck JAVA_HOME against the real install folder, open a fresh terminal, and on Windows restart the computer if that new terminal still has the old Path.
A place to type the file
The first program is a few lines, so Notepad or any text editor is enough, and it is already on the machine. An IDE starts to earn its place once you want navigation, a debugger, and a project that knows which JDK you installed.
IntelliJ IDEA is the editor most Java teams settle on. Eclipse is the free IDE still common in older enterprise codebases. VS Code stays light if you add a Java extension, which makes it a comfortable step after a plain text editor. Stay with the text editor or VS Code until the terminal commands feel ordinary, then move to IntelliJ when you want the project tools.
Write the file, compile it, run it
1. Create the file. In a folder you can find again, save HelloWorld.java. The file name matches the class name, including the capital letters.
2. Put this in the file.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}public class HelloWorld declares the class the file is named after. public static void main(String[] args) is the method Java looks up when a program starts. System.out.println writes one line to the terminal and then moves to the next line.
3. Compile. In the terminal, change into that folder and run javac on the source file. The compiler writes HelloWorld.class beside it. That class file holds .
javac HelloWorld.java
4. Run. Pass the class name to java. The loads the class and prints the line.
java HelloWorld
Output
Hello, World!
Commands you will type again
Compile a file
javac FileName.java
Run a class
java FileName
Runtime version
java -version
Compiler version
javac -version
You now have a JDK on the machine and a program that printed. The next page, Java syntax, is where the same kind of file starts to hold variables and statements.