How to Set Up JDK and Print Hello World in Java
In this beginner tutorial, you will learn how to install the JDK on
Windows, configure the PATH environment variable (both temporary
and permanent), and write, compile, and run your first Hello World Java
program.
By the end, you will have a fully working Java development environment and understand
the difference between javac (compile) and java (run).
Download JDK from Oracle
Open the Oracle JDK download page. In 2026, the recommended version is Java 25 LTS (or Java 21 LTS for older dependencies).
Click on Java SE and go to the download page.
Click JDK Download to open the platform selection page.
Accept the license agreement, then download the JDK installer for your platform.
Extract the JDK
After the download completes, run the installer (or extract the ZIP file if you chose the archive). By default, JDK installs to:
C:\Program Files\Java\jdk-25(recommended, for new installations)D:\jdk-25(custom folder if you want to keep all JDKs together)
Wherever you install it, note the exact path — you will need it for the PATH variable.
The bin subfolder contains javac, java, and other
tools.
Set Temporary PATH
A temporary PATH only lasts for the current Command Prompt session. Once you close the window, the setting is lost.
Open Command Prompt and run the following:
Microsoft Windows [Version 10.0.19042.1110]
(c) Microsoft Corporation. All rights reserved.
C:\Users\Deep Singh> set path=D:\jdk-25\bin
C:\Users\Deep Singh> javac -version
javac 25
C:\Users\Deep Singh> java -version
java version "25" 2025-09-16 LTS
Java(TM) SE Runtime Environment (build 25+37-LTS-3491)
Java HotSpot(TM) 64-Bit Server VM (build 25+37-LTS-3491, mixed mode, sharing)
If you see output like the above, your temporary PATH is working. This is useful for quickly testing different JDK versions without permanently modifying your system.
Set Permanent PATH
To make the JDK available in every Command Prompt session, add it to the Windows environment variables.
Step 1: Open Environment Variables
Press Windows key, type env, and select Edit the system environment variables.
Step 2: Click "Environment Variables"
Step 3: Find the "Path" Variable
In the System variables section, scroll down and find the Path variable.
Step 4: Add the JDK bin Folder
Double-click Path, click New, and paste the full path
to the JDK's bin folder (e.g.,
D:\jdk-25\bin or C:\Program Files\Java\jdk-25\bin).
Step 5: Save and Close
Click OK → OK → OK to close all three dialog boxes. The PATH change takes effect in any newly opened Command Prompt window.
Verify Installation
Close all Command Prompt windows, open a fresh one, and run:
C:\Users\Deep Singh> javac -version
javac 25
C:\Users\Deep Singh> java -version
java version "25" 2025-09-16 LTS
Java(TM) SE Runtime Environment (build 25+37-LTS-3491)
Java HotSpot(TM) 64-Bit Server VM (build 25+37-LTS-3491, mixed mode, sharing)
If both commands return the expected version, your PATH is set up correctly.
Tip: If javac is not recognized, the PATH did not save.
Reopen Environment Variables and verify the entry points to the JDK bin
folder — not the root JDK folder.
Write, Compile, and Run Hello World
Open Notepad (or any text editor) and type the following program:
class Hello {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Save the file as Hello.java. If your class is declared public,
the file name must match the class name exactly — including case.
Commands to compile and run:
Save -> Hello.java
Compile -> javac Hello.java
Run -> java Hello
If you see Hello World printed on the console, you have successfully set up
Java and run your first program.
How It Works (Step-by-Step)
-
JDK contains everything — the Java Development Kit includes the
compiler (
javac), the runtime (java), and the JVM. -
PATH tells Windows where to find commands — when you type
javac, Windows searches every folder in PATH. Adding the JDKbinfolder makesjavacandjavaglobally available. -
Temporary PATH vs permanent PATH — temporary uses
set path=...in a single terminal session. Permanent adds the folder to Windows environment variables. -
javac compiles — it reads
Hello.javaand producesHello.class, which contains bytecode. -
java runs — it launches the JVM, loads
Hello.class, and executes themain()method. -
Class name = file name rule — a
publicclass must be in a file with the same name. Otherwise the compiler errors.
Frequently Asked Questions
Which JDK version should I install in 2026?
Install Java 25 LTS (released September 2025) for new projects. Java 21 LTS is also a safe choice if dependencies have not been tested with 25. Avoid non-LTS versions like Java 26 for production use.
What is the difference between JDK, JRE, and JVM?
JDK (Java Development Kit) contains the compiler, tools, and JRE — it is what developers install. JRE (Java Runtime Environment) contains the JVM and libraries needed to run Java programs. JVM (Java Virtual Machine) executes compiled bytecode. JDK includes JRE, and JRE includes JVM.
How do I set the permanent PATH for JDK on Windows?
Open Environment Variables, edit the Path variable, add a new entry
with the full path to the JDK bin folder (e.g.,
C:\Program Files\Java\jdk-25\bin), then click OK three times. Reopen
Command Prompt and run javac -version to verify.
Why does javac not work even after setting PATH?
You must close and reopen Command Prompt after changing the PATH.
Environment variables are only read when a new shell starts. Also verify the path points
to the bin folder, not the root JDK folder.
Why must the Java file name match the public class name?
Java requires that a public class be declared in a file with the same name
as the class. If the class is public Hello, the file must be
Hello.java. Non-public classes can be saved with any file name.
Conclusion
You have successfully set up the JDK on Windows, configured both temporary and permanent PATH variables, and run your first Hello World Java program. This foundation will carry you through every Java project you build.
For real development, use an IDE like IntelliJ IDEA, Eclipse, or VS Code with the Java Extension Pack. They handle PATH, compilation, and running automatically — but knowing the underlying commands is essential for debugging build issues.
Related topics: JDK vs JRE vs JVM, Java Introduction, First Java Program Explained, Best Java IDEs in 2026, How to Set JAVA_HOME on Windows, Java Environment Variables Guide
No comments:
Post a Comment