Java Basics, Java Programming · December 2, 2026

Java for Beginners, Lesson 2: Installing the JDK and Running Your First Program

Java for Beginners · Module 1: Getting Started


🎯 Why Setup Matters

A chef needs a kitchen before cooking. A Java programmer needs the JDK (Java Development Kit) before writing a single program. The JDK contains the compiler (javac) that turns your code into bytecode and the launcher (java) that runs it. Setup takes about ten minutes, and you only do it once.

📌 JDK = tools to write and run Java programs. JRE = only what is needed to run them. As a student you need the JDK. Lesson 3 explains the difference in detail.

1️⃣ Get Java Ready in Four Steps

1. Download
a JDK installer for your OS
➜
2. Install
run the installer, accept defaults
➜
3. Set PATH
so the terminal finds javac and java
➜
4. Verify
java -version and javac -version

Figure 1: The four steps to get Java ready on your computer.

Step 1: Download a JDK

Choose a recent LTS version (17, 21 or 25). Two good free sources are Oracle’s JDK download page and Adoptium (Eclipse Temurin), which is an open-source OpenJDK build. Pick the installer for your operating system.

Step 2: Install

  • Windows: run the downloaded installer (.msi or .exe) and accept the defaults. Some installers have an option such as “Set JAVA_HOME” and “Add to PATH”. Turn it on.
  • macOS: run the installer package, or install with a package manager such as Homebrew.
  • Linux: use your package manager, for example sudo apt install openjdk-21-jdk on Ubuntu or Debian.

Step 3: Make sure the PATH is set

The PATH is a list of folders your terminal searches when you type a command. If the JDK’s bin folder is not on it, the terminal says “javac is not recognized”. Installers that offer the PATH option do this for you. If yours did not, add the JDK’s bin folder to PATH in your system’s environment settings.

Step 4: Verify

Open a new terminal (Command Prompt, PowerShell or Terminal) and type these two commands. Each should print a version number. The exact number depends on what you installed.

java -version
javac -version
⚠️ Always open a NEW terminal after installing. A terminal that was already open does not see the new PATH.

2️⃣ Your Working Folder

Create a folder just for your programs, for example JavaPrograms, and open your terminal inside it. Keeping files together makes compile and run commands short.

3️⃣ Compile and Run: The Real Process

Java has two commands where C had one. First you compile, then you run.

Welcome.java
you write this
⬇ javac Welcome.java (compile)
Welcome.class
bytecode made by javac
⬇ java Welcome (run, no .class)
Output on screen

Figure 2: Compile with javac, then run with java.

Program 1: Hello, World! Save this as HelloWorld.java.

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
🖥️ Output:
Hello, World!

In the terminal you would type:

javac HelloWorld.java
java HelloWorld
  • javac HelloWorld.java compiles. If there are no errors, it prints nothing and creates HelloWorld.class.
  • java HelloWorld runs the program. Write the class name only, without .java or .class.
  • The file name must match the public class name exactly, including capital letters.
Folder: JavaProgramsBefore javacAfter javac
Welcome.java✔ present✔ present
Welcome.class✘ not yet✔ created by javac

Figure 3: javac creates a .class file next to your source file.

Program 2: Reading words typed after the command

public class Welcome {
    public static void main(String[] args) {
        System.out.println("Number of arguments: " + args.length);
        if (args.length > 0) {
            System.out.println("Welcome, " + args[0] + "!");
        } else {
            System.out.println("Welcome, stranger!");
        }
    }
}
🖥️ Output of: java Welcome:
Number of arguments: 0
Welcome, stranger!
🖥️ Output of: java Welcome Ajay:
Number of arguments: 1
Welcome, Ajay!
  • args holds the words you type after the class name. This is the same idea as command-line arguments in C.
  • args.length says how many words were typed. With none, the program takes the else branch.
  • args[0] is the first word. Counting starts at 0.
💡 Shortcut (Java 11 and newer): you can run a single-file program in one step with java HelloWorld.java. It compiles in memory and runs. Still learn the two-step way, because bigger projects use it.

4️⃣ Choosing a Tool to Write Code

You can write Java in any text editor, but a code editor or IDE (Integrated Development Environment) highlights mistakes and saves time.

ToolTypeGood for
Notepad + terminalPlain editorLearning how compile and run really work
VS CodeLight editorBeginners; add the Extension Pack for Java
IntelliJ IDEA CommunityFull IDEBigger projects; smart suggestions
EclipseFull IDELong used in colleges and companies
NetBeansFull IDESimple and beginner friendly

Figure 4: Common tools for writing Java.

Our advice: write your first five programs with a simple editor and the terminal so you understand javac and java. Then move to an IDE.

5️⃣ The Three Errors Every Beginner Sees

Error 1: A missing semicolon

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello")
    }
}
🖥️ Compiler message:
Hello.java:3: error: ‘;’ expected
System.out.println(“Hello”)
^
1 error
  • Read the line number (3) and the arrow. The compiler points just after the place where the semicolon should be.

Error 2: File name does not match the class name (this code is saved in a file named Hello.java)

public class Hi {
    public static void main(String[] args) {
        System.out.println("Hello");
    }
}
🖥️ Compiler message:
Hello.java:1: error: class Hi is public, should be declared in a file named Hi.java
public class Hi {
^
1 error
  • A public class must live in a file with the same name. Rename the file or the class so they match.

Error 3: Wrong capital letter

public class Hello {
    public static void main(String[] args) {
        system.out.println("Hello");
    }
}
🖥️ Compiler message:
Hello.java:3: error: package system does not exist
system.out.println(“Hello”);
^
1 error
  • Java is case-sensitive. System must start with a capital S.
⚠️ One more runtime error: running java Hello when no Hello.class exists prints Error: Could not find or load main class Hello. Check that you compiled first and that you are in the right folder.

⚠️ Common Setup Problems

ProblemLikely causeFix
“javac is not recognized”JDK’s bin folder is not on PATH, or the terminal was open during installFix PATH, then open a new terminal
java -version works but javac does notOnly a JRE is installedInstall the full JDK
“Could not find or load main class”Wrong folder, not compiled, or .class typed in the commandCompile first; run java ClassName
Two different versions show upSeveral JDKs installedPut the one you want first on PATH
Editor saved Hello.java.txtWindows hides known extensionsTurn on “File name extensions” in File Explorer

❓ Frequently Asked Questions

Q1. Which Java version should I install?
Any recent LTS: 17, 21 or 25. All examples in this series work on them.

Q2. Do I need an IDE?
No. A text editor and a terminal are enough, and teach you the basics.

Q3. What is JAVA_HOME?
An environment variable that points to the JDK folder. Some tools need it. Many installers set it for you.

Q4. Can I install more than one JDK?
Yes, but the first one on PATH is the one the terminal uses.

✅ Key Takeaways

  • Install the JDK, not just the JRE, and choose an LTS version.
  • Make sure the JDK’s bin folder is on the PATH, then open a new terminal.
  • Check the setup with java -version and javac -version.
  • Compile with javac File.java. Run with java ClassName.
  • The file name must match the public class name, and Java is case-sensitive.
  • Read compiler messages: the line number and the arrow show where to look.
🎓 Practice questions: (1) What is the difference between javac and java? (2) Which file does javac Welcome.java create? (3) Why do we write java Welcome and not java Welcome.class? (4) Install the JDK and run java -version. What version do you see? (5) Create the three errors from this lesson on purpose and read each message.
All 30 lessons in this series (open course page)

Module 1 · Getting Started

Module 2 · Building Blocks

Module 3 · Operators

Module 4 · Control Flow

Module 5 · Data and Methods

Module 6 · Objects and Revision