Topic 0.4
Install the JDK and Run Your First Program
In one line
Install a free JDK (Eclipse Temurin), check it with java -version and javac -version, then write Main.java, compile it with javac Main.java and run it with java Main. Since Java 11 you can also skip the separate compile step and run java Main.java directly.
Think of it like this
Getting a new kitchen ready. Before you can cook (run programs), you need the kitchen tools (the JDK) installed and somewhere you can reach them (the PATH). Then you write the recipe (Main.java), prepare it (javac compiles it), and cook it (java runs it). The first recipe everyone cooks is the same tiny one: printing "Hello, World!".
Words you'll meet
New words in this topic, in plain English. Come back here whenever one feels fuzzy.
- Terminal
- A window where you type commands and see their text output. PowerShell on Windows, Terminal on macOS and Linux.
- Command
- A line you type in a terminal to run a program, such as
javac Main.java. - Folder (directory)
- A place on disk that holds files. The terminal always has a current folder, where commands look for files.
- PATH
- An environment variable listing the folders where your computer looks for commands. The JDK's
binfolder must be on it. - JAVA_HOME
- An environment variable that points to the JDK's main folder. Build tools and IDEs use it to find Java.
- Environment variable
- A named setting the operating system gives every program, such as
PATHorJAVA_HOME. - Compile
- Translate source code into another form.
javaccompiles.javafiles into.classfiles. - IDE
- Integrated Development Environment: an editor with Java built in, such as IntelliJ IDEA or VS Code with Java extensions. It compiles and runs for you.
- Eclipse Temurin
- A free, open-source JDK distribution from the Eclipse Adoptium project.
Step by step
01Option 0: use the Run button in this course
You don't need to install anything to start. Every example marked runnable has a Run button that compiles and runs it in your browser with Java 17. You can edit the code and run it again. Use it for Phases 0 to 4 if you like.
You'll still want a real JDK soon: for Java 21 examples, for reading real error messages, and because real programs live on your own machine. The rest of this topic sets that up.
02Install Temurin on Windows
Easiest: open PowerShell and use winget, the Windows package manager that comes with Windows 10 and 11. Or download the .msi installer from adoptium.net, and in the installer turn on "Set JAVA_HOME variable" and "Add to PATH".
Close and reopen the terminal afterwards. A terminal reads PATH only when it starts, so an old window won't see the new java.
03Install Temurin on macOS and Linux
macOS: with Homebrew installed, one command installs Temurin. /usr/libexec/java_home -V lists every JDK on the Mac and where it lives.
Linux (Ubuntu or Debian): the system package openjdk-21-jdk is an OpenJDK build that works the same for this course. Temurin's own package temurin-21-jdk is available after adding the Adoptium repository (instructions on adoptium.net). On Fedora the package is java-21-openjdk-devel. SDKMAN! (sdk install java ...) is a popular way to install and switch between many JDKs on macOS and Linux.
04Check the installation
Open a new terminal and ask both tools for their version. You should see the version you installed from both. If javac is missing but java works, you installed a JRE-only package or another JDK's java is first on your PATH.
To see which java your terminal picks, use where java (Windows) or which java (macOS and Linux). If the folder isn't the one you just installed, an older Java is earlier on the PATH.
05Write Main.java
Make a folder for your work (for example java-course), and inside it create a file named exactly Main.java. Use any plain-text editor: VS Code, Notepad, TextEdit in plain-text mode, or an IDE. Don't use a word processor such as Word; it adds hidden formatting.
Type the program below exactly, including capital letters, braces and the semicolon. Topic 0.5 explains every word. For now: public class Main names the class, main is where the program starts, and System.out.println prints a line.
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}06Compile with javac, run with java
In the terminal, go to your folder with cd java-course (cd means change directory). Then compile. If javac prints nothing, it worked: silence means success. A new file Main.class appears next to Main.java.
Now run it with java Main. You type the class name Main, not Main.class and not Main.java. The JVM finds Main.class in the current folder, loads it and calls its main method.
07The shortcut: java Main.java (Java 11+)
Since Java 11 the launcher can run a source file directly. java Main.java compiles the file in memory and runs it straight away. No Main.class is written to disk.
In this mode the file name doesn't even have to match the class name, and the first class in the file is the one that runs, so it must contain the main method. (Java 22 and later can also launch programs spread over several source files.) Use it for experiments and scripts. For bigger projects, compile properly so you only recompile what changed.
08Choose an editor
You can write Java in any text editor, but an IDE saves a lot of time: it shows errors as you type, completes names, and runs programs with one click. Popular free choices are IntelliJ IDEA Community Edition and VS Code with Microsoft's "Extension Pack for Java". Both find your installed JDK.
Learn the terminal way first anyway. When an IDE hides an error behind a red squiggle, knowing what javac and java actually do lets you understand it.
Try it yourself
- 1
Install and verify
Install Temurin 21 or 25 for your operating system, open a new terminal, and run
java -versionandjavac -version. Both should report the same feature version.terminal$ javac -version── expected output ──javac 21.0.4 - 2
Compile, list, run
Create
Main.javawith Hello World, compile it, and list the folder (diron Windows,lson macOS and Linux) to see the newMain.class. Run it withjava Main.terminal$ javac Main.javalsjava Main── expected output ──Main.class Main.javaHello, World! - 3
Make it yours
Change the program to print your own name card, like the third example. Predict exactly what will appear, including blank lines, then run it with
java Main.java.
Code & diagrams
The classic first program. Change the text between the quotes and run it again.
Expected output
Hello, World!print stays on the same line; println ends the line. An empty println() prints a blank line.
Expected output
Hello! My name is Asha.
I am learning Java today.
2 + 3 = 5
Have a great day!Programs can draw simple pictures with characters. Every line is one println.
Expected output
+-----------------+
| Name: Ravi |
| Age: 11 |
| Likes: chess |
+-----------------+Two-step: javac then java, leaves Main.class on disk. One-step (Java 11+): java Main.java, compiles in memory.
# Two steps (every Java version)
javac Main.java # writes Main.class
java Main # runs the class named Main
# One step (Java 11 and later)
java Main.java # compiles in memory, then runsBreak it on purpose
Errors are the best teachers. Make each change, read the error, guess what went wrong, then reveal the answer.
Break #1
File name doesn't match the public class
Save the Hello World program as Hello.java (the class inside is still public class Main) and compile it with javac.
Break #2
Run the file instead of the class
After compiling, run java Main.class instead of java Main.
Break #3
The terminal can't find javac
Install the JDK but forget to add it to PATH, or use a terminal that was open before the install, then type javac -version.
Myth vs fact
Myth
I have to pay for Java.
Fact
OpenJDK builds such as Eclipse Temurin, Amazon Corretto and Microsoft's build are free to use, including at work. Some vendors sell support contracts, which are optional.
Myth
java Main.java is a different, weaker kind of Java.
Fact
It is the normal compiler and JVM. The launcher just compiles in memory first. The program runs exactly as if you had used javac then java.
Myth
I need an IDE to write Java.
Fact
A text editor and a terminal are enough. IDEs make you faster, and you'll want one for big projects, but they run the same javac and java underneath.
Pro corner
Extra depth for experienced readers. New to this? Skip it for now and come back later.
- ▸
Keep several JDKs side by side and switch per project: SDKMAN! (
sdk use java ...) on macOS and Linux,/usr/libexec/java_home -v 21on macOS, or settingJAVA_HOMEandPATHper shell on Windows. Build tools can pin a toolchain version independently of the shell (Gradle toolchains, Maven toolchains), which avoids "works on my machine" version drift. - ▸
Single-file source launch (JEP 330, Java 11) compiles in memory and loads the result with an in-memory class loader, so no class file is written. It also supports shebang scripts on Unix: a file starting with
#!/usr/bin/java --source 21can be made executable and run directly. Java 22 (JEP 458) extended it to multi-file programs. - ▸
javasearches the class path forMain.class. By default that's the current folder, unless theCLASSPATHenvironment variable is set, which silently replaces the default. A staleCLASSPATHis a classic reason forClassNotFoundExceptionin a folder that clearly contains the class;java -cp . Mainoverrides it.
Remember this
- 1
The JDK is the one thing you install. This course recommends Eclipse Temurin from adoptium.net: free, open source, available for Windows, macOS and Linux, with long-term updates. Pick an LTS version: 21 or 25 (Topic 0.3). Choose the JDK package, not a JRE, because you need the compiler.
- 2
A terminal (also called a command line or shell) is a window where you type commands instead of clicking. On Windows it is PowerShell or Terminal, on macOS it is Terminal, on Linux it is usually a terminal app running bash. Everything in this topic is typed there.
- 3
Your computer finds commands like
javaby searching the folders listed in an environment variable calledPATH. Installers normally add the JDK'sbinfolder toPATH. Many tools (Maven, Gradle, IDEs) also look forJAVA_HOME, which should point to the JDK folder itself (the folder that containsbin). - 4
The classic way has two steps.
javac Main.javacompiles your source intoMain.class.java Mainstarts the JVM and runs the class namedMain. Notice thatjavatakes the class name, not the file name: no.classon the end. - 5
Since Java 11,
java Main.javadoes both steps at once: it compiles the file in memory and runs it, leaving no.classfile behind. It is perfect for small programs and for learning. Real projects with many files use the two-step way, usually through a build tool (Topic 15.4). - 6
Java has a few strict rules that trip up every beginner on day one: the file name must match the
public classname exactly (Main.javaforpublic class Main), capital letters matter (Mainandmainare different), and every statement ends with a semicolon. Topic 0.5 explains each part of the program, and Topic 0.7 explains the error messages.
Explain it without notes
What is the difference between javac Main.java, java Main and java Main.java?
What are PATH and JAVA_HOME, and why does Java need them?
Why must the file holding public class Main be named Main.java?
You ran java -version and got 17, but you installed 21. What is going on and how do you fix it?
Practice
Write a program that prints a three-line greeting: your name, your favourite food, and your favourite colour.
Write a program that prints the word Java on one line using four separate System.out.print calls, one letter each, then ends the line.
Write a program that draws a small triangle of stars, three lines high (*, **, ***).
Trade-offs
- ↔
java Main.javais fastest to type and leaves no files around, but it recompiles every time and handles one file well.javacplusjavacompiles once and scales to many files. Real projects use a build tool on top. - ↔
Installing with a package manager (winget, Homebrew, apt, SDKMAN!) makes upgrades one command, while a manual installer gives you full control of the location. Package managers are usually the better default.
Done when you can
Done when
java -versionandjavac -versionboth show the JDK you installed.Done when you can explain what
PATHandJAVA_HOMEare for.Done when you can compile with
javac Main.javaand run withjava Main.Done when you can run a single file with
java Main.javaand say what is different.Done when you can fix a file-name mismatch and a "command not found" error.