Installing Java and Running Programs
In this lesson
Let’s get Java running on your computer. You will install the JDK, compile a program from the terminal, try JShell, and set up an editor.
Step 1: Install the JDK#
Download a free JDK build. Eclipse Temurin 21 (LTS) from adoptium.net is a great choice. Oracle JDK 21 also works.
- Windows: download the
.msiinstaller and tick “Set JAVA_HOME variable” and “Add to PATH”. - Mac: download the
.pkg, or runbrew install --cask temurin@21. - Linux (Ubuntu):
sudo apt install openjdk-21-jdk.
Step 2: Check the installation#
Open a new terminal (Command Prompt / PowerShell on Windows) and run:
java -version
javac -version
You should see something like openjdk version "21.0.4". If you see “command not found”, the JDK’s bin folder is not on your PATH. Reinstall with the PATH option ticked, or add it manually.
Step 3: Compile and run from the terminal#
Create a folder, and a file named Greeting.java inside it:
public class Greeting {
public static void main(String[] args) {
String name = "Asha";
System.out.println("Welcome to Java, " + name + "!");
}
}
In the terminal, go to that folder and run:
javac Greeting.java # creates Greeting.class (bytecode)
java Greeting # runs it on the JVM (no .class extension!)
Output:
Welcome to Java, Asha!
Shortcut: run a single file directly#
Since Java 11 you can skip the javac step for single-file programs:
java Greeting.java
Java compiles it in memory and runs it. Perfect for practice.
Step 4: JShell, Java’s playground#
JShell (Java 9+) lets you type Java and see results immediately, with no class or main needed:
jshell
jshell> int a = 7
a ==> 7
jshell> a * 6
$2 ==> 42
jshell> "java".toUpperCase()
$3 ==> "JAVA"
jshell> /exit
Use JShell to test small ideas quickly.
Step 5: Choose an editor (IDE)#
| Editor | Why |
|---|---|
| IntelliJ IDEA Community (free) | The most popular Java IDE: smart completion, refactoring, debugger. Recommended. |
| VS Code + “Extension Pack for Java” | Lightweight, good if you already use VS Code. |
| Eclipse | Older but still used in many companies. |
In IntelliJ: New Project → Java → choose JDK 21 → Create. Right-click the src folder → New → Java Class, type Main, then type psvm and press Tab: IntelliJ writes the main method for you. Type sout + Tab for System.out.println().
Learn these IntelliJ shortcuts early: psvm (main method), sout (println), Alt+Enter (quick fix), Ctrl+Alt+L (format code), Shift+F10 (run).
Command-line arguments#
The String[] args in main receives words typed after the class name:
public class Args {
public static void main(String[] args) {
System.out.println("You passed " + args.length + " arguments");
for (String a : args) {
System.out.println("- " + a);
}
}
}
java Args.java red green
You passed 2 arguments
- red
- green
Reading compiler errors#
Java’s compiler tells you the file, the line and the problem:
Greeting.java:4: error: ';' expected
System.out.println("Hi")
^
1 error
Read the first error first: later errors are often caused by it. Line 4, a semicolon is missing.
Common mistakes#
- Running
java Greeting.class(wrong) instead ofjava Greeting. - An old Java version on the PATH: check
java -versionandjavac -versionshow the same version. - Saving the file as
Greeting.java.txton Windows (turn on “show file extensions”).
Exercise#
- Install the JDK and confirm
java -versionworks. - Write
About.javathat prints three facts about you, then compile and run it withjavacandjava. - In JShell, calculate how many seconds are in a day.
- Modify
Args.javato print the arguments in reverse order.
Hint for 4
Loop from args.length - 1 down to 0: for (int i = args.length - 1; i >= 0; i--).
Summary#
- Install a JDK (Temurin 21), then check with
java -version. javac File.javacompiles;java ClassNameruns.java File.javadoes both for single files.- JShell is a quick playground; IntelliJ IDEA is the most popular IDE.
Next: variables and data types, the building blocks of every program.
Quick quiz
3 questions. Check what stuck and earn 2 XP for each right answer.
Progress is saved in this browser. Create a free account to keep it on every device.
Get new lessons in your inbox
One email when we publish a new tutorial or guide. No spam, unsubscribe in one click.