The JDK (Java Development Kit) is the foundation. Without it, you cannot compile or run Java programs. It includes the compiler (javac), the runtime (JVM), and all core Java libraries.
Always choose an LTS (Long-Term Support) version — it is stable and supported for years. Recommended: JDK 17 or JDK 21. Avoid odd-numbered versions like 19 or 23 — they are short-term releases.
Step 1 — Download
Two reliable, free sources:
| Source | URL | Notes |
|---|---|---|
| Eclipse Temurin (Recommended) | adoptium.net | Free, open-source, community-backed. Best for students. |
| Oracle JDK | oracle.com/java | Official Oracle release. Free for personal/dev use. |
| Amazon Corretto | aws.amazon.com/corretto | Free, enterprise-grade, maintained by Amazon. |
On the download page: select your operating system (Windows/Mac/Linux), your architecture (x64 for most PCs, aarch64 for Apple M1/M2), and download the installer (.msi for Windows, .pkg for Mac, .deb or .rpm for Linux).
Step 2 — Set Environment Variables
After installing, the system needs to know where Java is installed so that java and javac commands work from any folder in the terminal.
Windows — Setting JAVA_HOME and PATH:
- Right-click This PC → Properties → Advanced system settings
- Click Environment Variables button at the bottom
- Under System variables, click New:
Variable name:JAVA_HOME
Variable value:C:\Program Files\Java\jdk-17(your actual install path) - Find the Path variable → click Edit → click New → type:
%JAVA_HOME%\bin - Click OK on all dialogs. Open a new Command Prompt (important — old ones won't see the change).
C:\> java --version openjdk 17.0.9 2023-10-17 C:\> javac --version javac 17.0.9 C:\> echo %JAVA_HOME% C:\Program Files\Java\jdk-17
macOS — Using .pkg installer (easiest):
- Run the downloaded
.pkgfile — it installs automatically to/Library/Java/JavaVirtualMachines/ - Open Terminal and add to your shell profile. For Zsh (default on modern Mac), edit
~/.zshrc:
# Add to ~/.zshrc (open with: nano ~/.zshrc) export JAVA_HOME=$(/usr/libexec/java_home -v 17) export PATH=$JAVA_HOME/bin:$PATH # Apply changes: source ~/.zshrc # Verify: java --version javac --version
Linux (Ubuntu/Debian) — Using apt:
# Update packages and install JDK 17: sudo apt update sudo apt install openjdk-17-jdk # Verify installation: java --version javac --version # If multiple Java versions exist, choose default: sudo update-alternatives --config java # Set JAVA_HOME (add to ~/.bashrc): export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 export PATH=$JAVA_HOME/bin:$PATH source ~/.bashrc
Step 3 — Verify Installation
Open a brand new terminal window and run both commands. Both must show version numbers:
$ java --version openjdk 17.0.9 2023-10-17 OpenJDK Runtime Environment Temurin-17.0.9+9 $ javac --version javac 17.0.9
Both java and javac working means JDK is installed and your PATH is correct. You are ready to compile and run Java programs.
- Windows: Did you open a NEW Command Prompt after setting PATH? Old ones don't see new variables.
- All OS: Check that JAVA_HOME points to the correct folder where JDK was installed.
- All OS: Make sure you added
%JAVA_HOME%\bin(Windows) or$JAVA_HOME/bin(Mac/Linux) — the\binpart is essential. - Mac: Did you run
source ~/.zshrcafter editing the file?
An IDE (Integrated Development Environment) is a smart code editor with features like autocomplete, error highlighting, and one-click running. You can write Java in Notepad — but a good IDE makes you 10x more productive.
| IDE | Best For | Pros | Cons | Cost |
|---|---|---|---|---|
| IntelliJ IDEA Community | Professional & students | Best Java intelligence, smart autocomplete, refactoring, built-in debugger | Higher RAM usage (~500MB+) | Free |
| VS Code | Lightweight daily use | Fast, free, great extensions ecosystem, low RAM | Needs extensions, not Java-native | Free |
| Eclipse | University & enterprise | Widely used in academia, powerful features | Older UI, can feel slow | Free |
| BlueJ | Complete beginners | Designed for learning OOP, shows object diagrams | Not used in real-world work | Free |
| Replit / JDoodle | Quick experiments | No installation, runs in browser | Limited features, needs internet | Free |
Use IntelliJ IDEA Community Edition for daily work and assignments — it has the best Java support and will be familiar when you enter the industry. Use VS Code when on a low-RAM machine. Know how to run from the terminal too — university lab exams may not have IDEs.
IntelliJ IDEA — Complete Setup
jetbrains.com/idea → Download Community Edition (free). Run the installer, accept defaults.JavaCourse) → Click Create.HelloWorld → Press Enter.psvm and press Tab — IntelliJ auto-expands it to the full main method! Type sout + Tab for System.out.println().| Type This | Press | Expands To |
|---|---|---|
psvm | Tab | public static void main(String[] args) { } |
sout | Tab | System.out.println(); |
soutv | Tab | System.out.println("varName = " + varName); |
fori | Tab | Full for(int i=0; i<n; i++) loop |
iter | Tab | Enhanced for-each loop |
VS Code — Java Setup
code.visualstudio.com. Free, fast, works on all OS.HelloWorld.java. VS Code will detect it as Java automatically.You should know all three methods. University lab exams may only have a terminal. Job interviews may ask you to compile from command line.
Way 1 — Terminal / Command Prompt (Master This First)
# Step 1: Navigate to the folder containing your .java file cd Desktop/JavaProjects # Step 2: List files to confirm your .java file is there ls # Mac/Linux dir # Windows # Step 3: Compile — creates HelloWorld.class in the same folder javac HelloWorld.java # Step 4: Run — use class name, NOT filename java HelloWorld # Output: Hello, World!
| Command | Windows | Mac / Linux |
|---|---|---|
| Go into folder | cd FolderName | cd FolderName |
| Go up one level | cd .. | cd .. |
| List files | dir | ls |
| Clear screen | cls | clear |
| Show current path | cd | pwd |
Way 2 — IntelliJ IDEA (Fastest for Development)
Once set up (see Section 2.2), simply press Shift+F10 or click the green play button. IntelliJ handles compilation and running automatically. You can see output, errors, and debug all in one window.
| Shortcut | Action |
|---|---|
Shift+F10 | Run current program |
Shift+F9 | Debug current program |
Ctrl+Z | Undo |
Ctrl+/ | Toggle comment on line |
Ctrl+D | Duplicate current line |
Alt+Enter | Quick fix (fix errors automatically) |
Ctrl+Shift+F10 | Run the file in the editor |
Way 3 — Online Compiler (No Installation Needed)
Useful when you're on a different computer or want to test quickly. Just paste code and run.
| Site | URL | Best Feature |
|---|---|---|
| JDoodle | jdoodle.com | Simple, fast, supports multiple Java versions |
| Replit | replit.com | Full project support, shareable links |
| OnlineGDB | onlinegdb.com | Has debugger, good for tracing code |
| Programiz | programiz.com/java-programming/online-compiler | Clean UI, good for beginners |
Keeping your files organised from day one is a professional habit. Here's a structure that works for university coursework:
In IntelliJ, code goes inside the src/ folder of your project. The compiled .class files go to out/production/ automatically. You never need to run javac manually in IntelliJ — it compiles in the background as you type.
Every beginner hits these errors. Here they are with exact causes and fixes:
| Error Message | Cause | Fix |
|---|---|---|
| 'java' is not recognized | PATH not set / terminal not restarted | Set JAVA_HOME + PATH, open NEW terminal |
| 'javac' is not recognized | JRE installed instead of JDK, or PATH missing | Install JDK (not JRE), add bin to PATH |
| error: class X is public, should be in file X.java | Filename doesn't match class name | Rename file to exactly match class name (case-sensitive) |
| error: ';' expected | Missing semicolon at end of statement | Add ; at the line shown in error |
| error: reached end of file while parsing | Missing closing brace } | Count opening vs closing braces — add the missing one |
| Could not find or load main class X | Wrong class name in run command, or in wrong folder | Use java ClassName (no .class). Run from folder containing .class file |
| Main method not found in class X | main method missing or wrong signature | Ensure exact signature: public static void main(String[] args) |
| error: cannot find symbol | Variable/method not declared, or typo | Check spelling, ensure variable is declared before use |
Java errors show: FileName.java:LineNumber: error: description
HelloWorld.java:5: error: ';' expected
System.out.println("Hello")
^
1 error- HelloWorld.java — which file has the error
- 5 — which line number to look at
- error: ';' expected — what Java expected but didn't find
- ^ — caret points to the exact position of the problem
Always fix errors from top to bottom — one error can cascade into many. Fix the first one and recompile before worrying about the others.
Tick every item to confirm your environment is fully working:
Q: What is JAVA_HOME and why must it be set?
Answer: JAVA_HOME is an environment variable that stores the installation path of the JDK on the system. It must be set so that the operating system and other tools (like build tools Maven and Gradle) can locate the JDK directory. The PATH variable is then set to include %JAVA_HOME%\bin (Windows) so that java and javac commands can be executed from any directory in the terminal.
Q: What is the difference between compiling and running a Java program? Write the exact commands used.
Answer: Compiling is the process of translating Java source code (.java file) into bytecode (.class file) using the Java compiler. Command: javac HelloWorld.java — this creates HelloWorld.class. Running is the process of launching the JVM to execute the compiled bytecode. Command: java HelloWorld — note there is no file extension; Java takes the class name, not the filename. The JVM then loads the .class file, finds the main method, and executes it.
MCQs
Test.java?java Test.javajavac Test.javajava Testcompile Test.javajavac is the Java compiler command. javac Test.java compiles the source file and produces Test.class. The java command is for running, not compiling.HelloWorld.java, which file is created?javac produces a .class file containing bytecode. This is NOT machine code — it's an intermediate format the JVM can run on any platform.psvm expand to in IntelliJ?System.out.println();public static void main(String[] args) { }psvm is a Live Template in IntelliJ that expands to the full main method signature. Type it and press Tab. Similarly, sout + Tab expands to System.out.println();.'javac' is not recognized as an internal or external command. Most likely cause?javac executable. Either the JDK is not installed, the bin folder was not added to PATH, or the terminal wasn't restarted after setting PATH.MyApp?java MyApp.javajavac MyAppjava MyAppjava MyApp.classjava MyApp is correct — no extension. The java launcher takes the class name. java MyApp.java would try to find a class literally named "MyApp.java". java MyApp.class would try to find a class named "MyApp.class".- javac vs java: "javac = compile (C for Compile). java = run."
- No extension when running:
java HelloWorldnotjava HelloWorld.class— you give the class NAME, not the filename. - Fix errors top to bottom: One error often causes 10 more. Fix the first, recompile, repeat.
- New terminal after PATH change: Environment variables are loaded when the terminal starts — old terminals don't see new values.
- psvm + Tab: The fastest way to type the main method in IntelliJ. sout + Tab for println.
adoptium.net or oracle.com. Choose LTS version (17 or 21).JAVA_HOME to JDK install path. Add $JAVA_HOME/bin to PATH. Open NEW terminal to apply.java --version and javac --version must both show version numbers.javac FileName.java → creates FileName.class. Run: java ClassName (no extension).psvm+Tab → main method. sout+Tab → println. Shift+F10 → run.Easy
java --version and javac --version in your terminal. Take note of the version numbers. If either fails, fix it before continuing.HelloWorld.java using only a plain text editor (Notepad or gedit — no IDE). Compile and run it entirely from the terminal. Confirm output.psvm and sout shortcuts to write a program that prints your name. Run with Shift+F10.Medium
First.java, Second.java, Third.java — each printing a different message. Compile all three with one command: javac *.java. Then run each.Hard
ls $JAVA_HOME/bin on Mac/Linux, dir %JAVA_HOME%\bin on Windows). Identify at least 8 tools listed and look up what each one does./** */ Javadoc comment above the class declaration. Then run javadoc ClassName.java in the terminal. Open the generated HTML file. This is how Java API documentation is made.True setup mastery means you can recover from anything.
- Unset your PATH temporarily (or open a fresh shell without Java in PATH) and try running Java. Reproduce the "not recognized" error, then fix it from scratch.
- Without any IDE: write, compile, and run a program that prints a 5-line formatted table using only a plain text editor and terminal commands.
- Find out: what Java version is installed on your university lab computers? Is it JDK or JRE? How can you tell the difference from the command line?