🌱BeginnerJava Foundations

Setting Up Java

Install the JDK, choose an IDE, and get your development environment ready to write and run Java programs.

Overview

Before writing Java code locally you need two things: the Java Development Kit (JDK) and an editor or IDE. The JDK contains the compiler (javac) that turns your source code into bytecode, and the JRE (Java Runtime Environment) that runs it. For beginners the fastest start is an online compiler - no installation needed. Sites like onecompiler.com/java or replit.com let you write and run Java instantly. When you are ready for a local setup, IntelliJ IDEA Community Edition and Visual Studio Code (with the Extension Pack for Java) are the most popular free choices. The current long-term support (LTS) version of Java is Java 21. Always prefer an LTS release for learning so you have stable, well-documented behavior.

Key Points

  • β€’JDK (Java Development Kit) includes the compiler (javac) and standard library - you need this to develop
  • β€’JRE (Java Runtime Environment) can only run Java programs; the JDK includes the JRE
  • β€’Online compilers (onecompiler.com/java, replit.com) let you start immediately with zero setup
  • β€’IntelliJ IDEA Community Edition is the most popular free Java IDE with excellent tooling
  • β€’VS Code + "Extension Pack for Java" is a lighter alternative to IntelliJ
  • β€’Java 21 is the current LTS release - use it for learning to get the latest stable features

Examples

Verify Java is installed (run in your terminal)

# Check your Java version
java -version

# Check the compiler version
javac -version

If both commands print version numbers, your JDK is correctly installed.

Compile and run a Java file from the terminal

# 1. Save your code as HelloWorld.java
# 2. Compile it
javac HelloWorld.java

# 3. Run the compiled class (no .class extension)
java HelloWorld

The class name must exactly match the filename (including case). HelloWorld.java β†’ public class HelloWorld.

A standard project file structure in IntelliJ IDEA

MyProject/
β”œβ”€β”€ src/
β”‚   └── Main.java       ← your source files go here
β”œβ”€β”€ out/
β”‚   └── Main.class      ← compiled bytecode (auto-generated)
└── MyProject.iml       ← IntelliJ project file

IntelliJ handles compilation automatically when you click Run.

Common Mistakes

  • Installing only the JRE instead of the JDK - the JRE cannot compile code, you need the full JDK
  • File name does not match class name - HelloWorld.java must contain `public class HelloWorld`
  • Running `java HelloWorld.class` instead of `java HelloWorld` - omit the .class extension when running

Note

If you just want to experiment quickly, skip the local setup for now. Go to onecompiler.com/java, type your code, and hit Run. You can install IntelliJ IDEA later when you are ready to build larger projects.

Exercises

Write Java code and click Run to execute it instantly. When your output looks correct, mark as done.

Loading editor…

Completed: 0 / 4 exercises

Knowledge Check

Question 1 of 4

What is the difference between the JDK and the JRE?