⚙️ How to Install the JDK and Run Your First Java Program
Install the Java JDK on Windows, macOS or Linux and run your first program. Set JAVA_HOME, pick an IDE (IntelliJ, VS Code, Eclipse), and understand the compile-run cycle.
You need two things: a JDK and an editor.
📥 Install a JDK
- OpenJDK (free, recommended) — download from Adoptium (Temurin), or your package manager:
sudo apt install openjdk-21-jdk(Linux),brew install openjdk(macOS). - Oracle JDK — also free for development; some licensing nuances for production.
- Verify:
java -versionandjavac -version.
🔧 Set JAVA_HOME
Many tools (Maven, Gradle, IDEs) need the JAVA_HOME environment variable pointing at your JDK install directory. On Windows set it in System Environment Variables; on macOS/Linux export it in your shell profile.
💻 Pick an editor
- IntelliJ IDEA — the industry standard, best autocomplete and refactoring. Community edition is free.
- VS Code + Extension Pack for Java — lightweight, great for beginners.
- Eclipse — mature, enterprise-heavy.
⚡ The build cycle
Behind every IDE 'Run' button: javac compiles your .java to .class, then java runs it. Real projects use Maven or Gradle to manage dependencies and the build — covered later.
💻 Code Examples
Check your install
java -version
javac -versionOutput:
openjdk version "21.0.2" ... javac 21.0.2
Compile and run from the terminal
// Hello.java
public class Hello {
public static void main(String[] args) {
System.out.println("Setup works!");
}
}
// terminal:
javac Hello.java
java HelloOutput:
Setup works!
⚠️ Common Mistakes
- Installing only the JRE — you can't compile without the JDK's javac.
- Forgetting to set JAVA_HOME — Maven/Gradle and some IDEs fail to find the JDK.
- Mixing JDK versions across projects — use a version manager (SDKMAN!) to switch cleanly.
- Running `java Hello.java` on very old JDKs — single-file source launch needs JDK 11+.
🎯 Interview Questions
Real questions asked at top product and service-based companies.
Q1.What is JAVA_HOME and why is it needed?Beginner
An environment variable pointing at the JDK install directory. Build tools (Maven, Gradle) and IDEs read it to locate the compiler and runtime.
Q2.What's the difference between OpenJDK and Oracle JDK?Beginner
Both implement the same Java spec. OpenJDK is the free, open-source reference; Oracle JDK is Oracle's build with commercial support options. For most developers, OpenJDK (e.g., Temurin) is the standard choice.
Q3.What do Maven and Gradle do?Intermediate
They are build tools that manage dependencies, compile, test, and package Java projects. Maven uses XML (pom.xml); Gradle uses a Groovy/Kotlin DSL and is faster for large builds.
Q4.What is the difference between javac and java?Beginner
javac is the compiler — it turns .java source into .class bytecode. java is the launcher — it starts the JVM and runs that bytecode.
🧠 Quick Summary
- Install a JDK (OpenJDK/Temurin recommended), not just the JRE.
- Verify with `java -version` and `javac -version`.
- Set JAVA_HOME for build tools and IDEs.
- IntelliJ IDEA or VS Code are the best editors to start.
- javac compiles; java runs. Maven/Gradle automate real builds.