Java JDK Setup: Install, Verify and Run Your First Program

Install Java with proof that your compiler and runtime agree. This tutorial separates JDK, JRE and JVM, then traces one program from source to exact output.

KnowledgeGate Team

Exam prep & CS education

Updated 9 Sep 20266 min read

Installing "Java" is not enough if the terminal runs java but cannot find javac, or an IDE silently uses a different JDK. Prove the setup: distinguish JDK, JRE and JVM, check PATH and JAVA_HOME, compile a fixed program, and trace its output. For Java, C, C++, Python and DSA, see Coding & Skill Development Courses.

1. Java, JDK, JRE and JVM: know what you are installing

Java is the language used in JdkSetupCheck.java. The JDK is the developer kit supplying javac, java and other tools. The JVM is the execution engine running bytecode from JdkSetupCheck.class. The JRE is the runtime component set already included in a full JDK.

Name

Role

Proof

JDK

Provides javac and runtime tools

Both version commands work

javac

Compiles source into bytecode

A new .class file appears

JVM

Executes the bytecode

The program output appears

PATH

Lets the shell locate executables

Location checks find both commands

JAVA_HOME

Points tools and IDEs to the JDK root

The home check prints that root

The JVM does not compile source, and these are not three unrelated downloads. Follow JdkSetupCheck.java -> javac -> JdkSetupCheck.class -> java launcher -> JVM -> output.

2. Install a full JDK and connect it to the terminal

Use the JDK version required by your course, project or team. Getting Started with Java links current JDK builds and installation guidance. Choose the package for your operating system and processor, run its installer or package-manager command, and record the installed JDK root. A runtime-only installation cannot provide javac.

Platform

Example JDK 21 root

What to remember

Windows

C:\Program Files\Java\jdk-21

Your vendor or installer may choose another path

macOS

/Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home

Use the root detected on your machine

Linux

/usr/lib/jvm/jdk-21

Replace this example with the installed root

The exact root depends on the JDK build and installer. JAVA_HOME points to that root, not its bin directory. Add the root's bin directory to PATH so the shell finds both commands.

On Windows, open Edit the system environment variables, create JAVA_HOME, and add %JAVA_HOME%\bin to Path. For an installation at the example root, the values are:

Code
JAVA_HOME=C:\Program Files\Java\jdk-21
PATH entry=%JAVA_HOME%\bin

On macOS, ask the operating system for the installed JDK 21 root:

bash
export JAVA_HOME=$(/usr/libexec/java_home -v 21)
export PATH="$JAVA_HOME/bin:$PATH"

For the example Linux root:

bash
export JAVA_HOME=/usr/lib/jvm/jdk-21
export PATH="$JAVA_HOME/bin:$PATH"

The macOS and Linux export commands affect the current shell. Add them to your shell's startup file only when you want that JDK selected in future terminals, then open a new terminal.

3. Verify the JDK before opening an IDE

Run four checks in the new terminal:

  1. Runtime: java -version

  2. Compiler: javac -version

  3. Home: echo %JAVA_HOME% on Windows, or echo "$JAVA_HOME" on macOS and Linux

  4. Locations: where java and where javac on Windows, or command -v java and command -v javac on macOS and Linux

A consistent JDK 21 result could show 21.0.2 from java -version, javac 21.0.2 from javac -version, the chosen JDK root from JAVA_HOME, and both executables inside that root's bin directory. Your patch number may differ; the feature versions and resolved JDK should agree.

Pass only if both commands exist, their feature versions match, and their paths reach the intended JDK. If java works but javac does not, repair the JDK or PATH before installing an IDE. An IDE wraps the toolchain, it does not replace it.

4. Fully worked example: compile and run JdkSetupCheck

Create an empty java-start folder, open a terminal there, and save this exact file as JdkSetupCheck.java. The public class and file names must match in capitalisation.

java
public class JdkSetupCheck {
    public static void main(String[] args) {
        int lessonsCompleted = 3;
        int minutesPerLesson = 25;
        int totalMinutes = lessonsCompleted * minutesPerLesson;

        System.out.println("JDK ready: " + totalMinutes + " minutes");
    }
}

Compile it:

bash
javac JdkSetupCheck.java

Success normally prints no message, returns to the prompt, and creates JdkSetupCheck.class beside the source. Run the class without .java or .class:

bash
java JdkSetupCheck

The only output line is:

Code
JDK ready: 75 minutes

Trace every value. lessonsCompleted = 3 and minutesPerLesson = 25, so 3 x 25 = 75. Concatenation builds JDK ready: 75 minutes, and println adds a line break. Change nothing first.

5. What happens between source code and output

The editor saves Unicode source in JdkSetupCheck.java. javac checks syntax, names and types, then emits bytecode in JdkSetupCheck.class. The java launcher starts a JVM and requests JdkSetupCheck. The class loader finds it on the current class path, verification checks the bytecode shape, and main(String[] args) prints the result.

The compiler first recognises units such as keywords, identifiers, literals and operators. Lexical Analysis in Compiler Design: Tokens and Lexemes explains that stage in more depth.

Source targets the JVM class-file format, while each JVM targets an operating system and processor. A compatible .class can run across platforms with compatible runtimes, but may fail on an older runtime. Native libraries are not automatically portable.

javac compiles JdkSetupCheck.java into JdkSetupCheck.class bytecode, which the JVM runs to print JDK ready: 75 minutes.

6. Repair the five setup and first-run failures learners actually see

Check the toolchain before changing code.

Symptom

Likely cause

Check

Fix

javac is not recognised or not found

JDK bin is absent from PATH

Run the compiler and location checks

Add <JAVA_HOME>/bin to PATH

java works but javac does not

The shell finds a runtime-only or different installation

Compare both resolved paths

Make both commands resolve to the intended JDK

class JdkSetupCheck is public, should be declared in a file named JdkSetupCheck.java

File name or case is wrong

Inspect the public class and file name

Make both exactly JdkSetupCheck

Could not find or load main class JdkSetupCheck

Wrong folder, extension or class path

Check the current folder and command

Run from java-start as java JdkSetupCheck

UnsupportedClassVersionError

A newer JDK compiled the class than the runtime loading it

Compare java and javac versions and paths

Align both commands with the intended JDK

Debug in order. Rerun Section 3, confirm both files, delete only this exercise's stale .class, recompile, then run java JdkSetupCheck from java-start. For version mismatch, repair command resolution first.

The entry point is public static void main(String[] args). Java is case-sensitive, so jdkSetupCheck, JdkSetupCheck and JDKSetupCheck differ. Rename the source file with its public class.

A flowchart checking javac, matching java versions, class creation and program output to confirm the JDK setup is verified.

7. How setup concepts are tested in questions and interviews

Typical checks distinguish JDK, JRE and JVM, identify compile and run commands, predict the generated file, trace output, diagnose path or version mismatches, and enforce file-name rules.

Test yourself:

  1. Which command creates bytecode from this source? javac JdkSetupCheck.java.

  2. Which file should appear? JdkSetupCheck.class.

  3. What does JAVA_HOME point to? The JDK root, not its bin directory.

  4. If the values become 4 and 30, what line prints? JDK ready: 120 minutes, because 4 x 30 = 120.

8. Java JDK setup: the short version and the next step

Use this sequence in a fresh terminal:

  1. Install a full JDK.

  2. Point JAVA_HOME to its root.

  3. Put its bin directory on PATH.

  4. Verify both version commands and resolved paths.

  5. Compile JdkSetupCheck.java.

  6. Run java JdkSetupCheck and confirm the exact 75-minute output.

Once it works, use Java Course: Concepts, MCQs and Coding Questions next. DSA using Java: Placement Preparation Course comes later if you can run basic Java and want interview-focused problem solving.

Change the values to 4 lessons and 30 minutes. Predict JDK ready: 120 minutes, recompile, run, and compare. Afterwards, Dynamic Programming Explained: 0/1 Knapsack is a future problem-solving read, not a prerequisite.