Course topics

By WebNest Studio Engineering Team

Java - Core Tutorial

Setting Up the Java Development Environment

Before writing a single line of Java, you need a working development environment: a JDK to compile and run code, and a place to write that code, whether that is a full IDE or a plain text editor paired with a terminal. Getting this right the first time avoids a long list of confusing errors later — "javac is not recognized," "java: command not found," or programs that mysteriously run an old cached version.

This setup only needs to be done once per machine (or once per JDK upgrade), and understanding what each step actually does will make you far more comfortable troubleshooting environment issues on a new laptop, a CI server, or a teammate's machine.

Why it matters

Most first-day Java problems are setup problems, not code problems: the wrong version on the PATH, a JRE without a compiler, or a file name that does not match its class. Being able to compile and run from a plain terminal means you can always tell whether a failure comes from your code or from your tools, even when an IDE hides those steps.

Watch: Java Environment Setup

Prefer to watch? This video walks through setting up your Java development environment. Follow it alongside the written steps below.

Watch on YouTube

Downloading and Installing a JDK

You need a JDK (Java Development Kit), not just a JRE, because the JRE has no compiler. Two common, free sources are the Eclipse Adoptium project (formerly AdoptOpenJDK, distributing the OpenJDK builds known as Temurin) and Oracle's own JDK builds, which are also free to use under Oracle's current no-fee license for recent versions. Either works for learning; Adoptium is a popular default because its license terms are simple and unambiguous for all use cases.

  • Go to the distributor's download page (e.g. adoptium.net) and pick a Long-Term Support (LTS) version such as JDK 17 or JDK 21 unless you have a specific reason to use a newer feature release.
  • Choose the installer that matches your operating system and CPU architecture (Windows x64, macOS ARM/Apple Silicon, Linux x64, etc.).
  • Run the installer. On Windows and macOS, the installer can usually configure environment variables for you if you check the relevant option; on Linux, extracting a tarball into a directory like /opt/java is common.

Setting JAVA_HOME and PATH

JAVA_HOME is an environment variable that many build tools (Maven, Gradle) and IDEs look up to find your JDK installation; it should point to the JDK's root folder (the one containing a "bin" subfolder), not to the "bin" folder itself. PATH is a separate environment variable that tells the operating system's shell where to look for executable programs — adding the JDK's "bin" directory to PATH is what lets you type "java" or "javac" directly in any terminal window without typing the full path. On Windows this is done through System Properties → Environment Variables; on macOS/Linux it is typically set by exporting the variables in a shell profile file such as ~/.zshrc, ~/.bashrc, or ~/.bash_profile.

Verifying the Installation

After installing and configuring the environment variables, open a fresh terminal window (existing ones will not see the new PATH) and run two commands to confirm both halves of the toolchain are visible: java -version checks the runtime launcher, and javac -version checks the compiler. Both should report the same version number; if only one works, or if they report different versions, PATH is likely pointing at more than one Java installation.

Choosing an IDE vs a Plain Text Editor

An IDE (Integrated Development Environment) such as IntelliJ IDEA or Eclipse bundles a code editor, a build system, a debugger, and project management into one application — it can detect a missing import, auto-complete method names, and let you set breakpoints and step through code visually. VS Code is a lighter-weight editor that becomes IDE-like for Java once you add Microsoft's "Extension Pack for Java." Alternatively, many learners start with a plain text editor and a terminal: you write the .java file yourself, then manually run javac and java. This "manual" workflow is slower for large projects but extremely valuable early on, because it forces you to understand the compile-then-run cycle instead of relying on a green "Run" button.

  • IntelliJ IDEA — widely used in industry, strong refactoring tools, excellent build-tool integration (Maven/Gradle).
  • Eclipse — free, mature, long history in enterprise Java development.
  • VS Code + Java extensions — lightweight, fast startup, good for smaller projects or polyglot workflows.
  • Plain text editor + terminal — no hidden magic; ideal for understanding exactly what javac and java do.

The Compile-Then-Run Cycle

Whichever tool you use, the same two-step cycle happens underneath: javac reads your .java source file, checks it for syntax and type errors, and — if it compiles cleanly — produces one or more .class files containing bytecode in the same directory. Then java (given the class name, not the file name, and without the .class extension) loads that bytecode and starts execution from its main method. An IDE's "Run" button is simply automating these two steps for you.

Example

Verifying the JDK installation and running the manual compile-then-run cycle

Java
// Terminal commands (not Java code) to verify setup:
// java -version
// javac -version

// File: EnvironmentCheck.java
public class EnvironmentCheck {
    public static void main(String[] args) {
        System.out.println("Java version property: " + System.getProperty("java.version"));
        System.out.println("Environment is ready.");
    }
}

// Terminal:
// javac EnvironmentCheck.java   -> produces EnvironmentCheck.class
// java EnvironmentCheck         -> runs the compiled bytecode
Output
Java version property: 21.0.1
Environment is ready.

Common Mistakes

  • Installing only a JRE (or relying on one bundled with another application) and then being confused that javac is missing — compiling requires a full JDK.
  • Setting JAVA_HOME to the "bin" folder instead of the JDK root folder, which breaks tools that expect to find "bin", "lib", and other subfolders under JAVA_HOME.
  • Editing PATH but continuing to test in an already-open terminal window — environment variable changes only apply to newly opened shell sessions.
  • Running "java MyProgram.java" with the .java extension or "java MyProgram.class" with the .class extension — the java launcher expects a bare class name.

Exercise

Confirm your installation from the terminal without using an IDE. Check both tools report a version, then compile and run the program below.

The last command should print Setup works.

Java · starter code
// Save this as SetupCheck.java
public class SetupCheck {
    public static void main(String[] args) {
        System.out.println("Setup works");
    }
}

// In a terminal, from the folder containing the file:
// 1. Check the runtime version
// 2. Check the compiler version
// 3. Compile the file
// 4. Run the class
Show hints
  • The runtime and the compiler are two separate commands: java and javac.
  • You compile a file name (with .java) but run a class name (without any extension).
Show solution
Java · solution
// 1. java -version
// 2. javac -version
// 3. javac SetupCheck.java      -> creates SetupCheck.class
// 4. java SetupCheck            -> prints: Setup works

Quick quiz

1. Which command compiles Hello.java?

  • A. java Hello.java
  • B. compile Hello.java
  • C. javac Hello
  • D. javac Hello.java
Show answer

D. javac is the compiler and takes the source file name including the .java extension.

2. What should the JAVA_HOME environment variable point to?

  • A. The JDK installation directory
  • B. The folder containing your .java files
  • C. The java.exe file itself
  • D. The folder containing your .class files
Show answer

A. JAVA_HOME points to the root of the JDK installation. Build tools such as Maven and Gradle use it to find the compiler and runtime.

3. After compiling, you type java Hello.class and get an error. What is the correct command?

  • A. javac Hello.class
  • B. java Hello
  • C. java Hello.java.class
  • D. run Hello
Show answer

B. The java launcher expects a class name, not a file name, so the .class extension must be left off.

Interview questions

What is the difference between PATH and CLASSPATH?

Show answer

PATH is an operating-system variable that tells the shell where to find executables, such as java and javac. CLASSPATH tells the Java compiler and the JVM where to look for compiled classes and JAR files that your program uses.

A wrong PATH gives "command not found"; a wrong CLASSPATH gives ClassNotFoundException or NoClassDefFoundError.

Key Points to Remember

  • A JDK (not just a JRE) is required for development because it includes the compiler, javac.
  • JAVA_HOME should point to the JDK root folder; PATH must include its "bin" subfolder so java/javac are found by name.
  • `java -version` and `javac -version` are the standard sanity checks after installation.
  • The core workflow is always compile-then-run: javac produces .class bytecode, and java executes it — an IDE just automates this cycle.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Runs plain Java 17. Frameworks such as Spring, and databases, are not available here. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering