Thuta Learning
Java
BasicProgrammingBeginner

Get Started (Setup)

JavaLesson 320 min

What you'll walk away with

  • Choose a JDK distribution at an LTS version and install it
  • Check that `java -version` and `javac -version` both work and report the same version
  • Compile and run a program with `javac File.java` and `java ClassName`
  • Diagnose setup errors such as 'not recognized' and 'Could not find or load main class'

Explain it simply

Setup in plain words

To write and run Java code you need a JDK (Java Development Kit). It contains the compiler `javac`, the `java` launcher that runs programs, and the standard library.

If you would rather skip setup for now, an online compiler such as Replit or OnlineGDB is a fine place to start. Once you build real projects, though, having a JDK and an editor on your own machine is far more convenient. After setup, the everyday routine is short: write a file, compile it, run it.

Problem → Why → Solution

The problem: the JDK is installed, but the terminal cannot find it

You run the JDK installer, press Run in your IDE, and it works. Then you open a terminal, type `javac`, and Windows says it has never heard of it. On some machines two JDKs end up installed, and the IDE uses 21 while the terminal uses 17.

If only the IDE works, the trouble surfaces later: on the day the project goes onto a build server, or a teammate clones it.

Command Prompt
C:\Users\aung> javac HelloWorld.java
'javac' is not recognized as an internal or external command,
operable program or batch file.

Why it happens

A terminal looks for commands only in the folders listed in the PATH environment variable. If the JDK's `bin` folder is not on PATH, `javac` cannot be found. The IDE works because it stores the JDK location in its own settings.

Core concept: a setup you can reproduce

A good setup is not one that happened to work once on your machine; it is one that gives the same result on any machine when you follow the same steps. These five steps get you there.

Pick a distribution and a version

Eclipse Temurin, Oracle JDK, Amazon Corretto and the Microsoft Build of OpenJDK are all built from OpenJDK, so any of them works. Choose an LTS version such as Java 21 or 25; this course's examples are checked on Java 21.

Install and check PATH

Most installers add the JDK's `bin` folder to PATH and point `JAVA_HOME` at the JDK folder; if yours does not, add them yourself. Build tools read `JAVA_HOME` to decide which JDK to use.

Verify both versions

Open a new terminal and run `java -version` and `javac -version`. Both must work and show the same version.

Choose an editor

Use IntelliJ IDEA, Eclipse, or VS Code with its Java extension. The JDK in the IDE's project settings must match the one in your terminal.

Compile and run

`javac HelloWorld.java` produces `HelloWorld.class`, and `java HelloWorld` runs that class on the JVM.

Know the three ways to run a program. The first runs `javac` and `java` as separate steps, and you can see the `.class` file on disk. The second, available since JDK 11, is `java HelloWorld.java`: it compiles the single file in memory and runs it immediately, leaving no `.class` file behind. The third is the IDE's Run button, which does the first approach for you behind the scenes.

For quick experiments there is also `jshell`, which ships with the JDK. You type an expression such as `2 + 3` without writing any class and see the result at once.

How to picture the setup
  1. Install the JDK
  2. PATH lets the shell find javac
  3. javac writes the .class file
  4. java runs the class on the JVM

`java` takes a class name, not a file name (except in source-launcher mode, where it takes a `.java` file).

General pattern: terminal commands

bash
java -version              # which JVM is on PATH
javac -version             # which compiler is on PATH (JDK only)
javac ClassName.java       # compile: writes ClassName.class
java ClassName             # run the class (no .class suffix)
java ClassName.java        # compile in memory and run (JDK 11+)

The first two lines check that setup is correct. The next two compile and run as separate steps, and the last line is a shortcut for single-file programs. Never add `.class` in `java ClassName`.

Compiling and running from the terminal

Save the code below with the exact name `HelloWorld.java`. The comments at the bottom are the two commands to type in the terminal. After running `javac`, check for yourself that `HelloWorld.class` has appeared in the folder.

java
// Save this file as HelloWorld.java
public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Compiled and running from terminal!");
  }
}

// Terminal commands:
// javac HelloWorld.java
// java HelloWorld

Code breakdown

  1. The file name is part of the contract// Save this file as HelloWorld.java

    A public class must live in a file with the same name. Save it as `Hello.java` and `javac` refuses with 'class HelloWorld is public, should be declared in a file named HelloWorld.java'.

  2. The class the JVM will loadpublic class HelloWorld {

    This class name is what you will type later in `java HelloWorld`. Java is case-sensitive, so typing `helloworld` will not find it.

  3. Step 1: compile// javac HelloWorld.java

    Checks the source file and writes `HelloWorld.class` containing bytecode. No output means success; you only see messages when something is wrong.

  4. Step 2: run// java HelloWorld

    Starts the JVM, loads the `HelloWorld` class and calls `main`. Type only the class name here, with no `.java` or `.class` extension.

You should see
Compiled and running from terminal!
What each command does
  1. Save HelloWorld.java
  2. javac checks it and writes HelloWorld.class
  3. java HelloWorld starts the JVM
  4. main runs and prints one line

PATH and names are right

Compiled and running from terminal!

PATH or a name is wrong

'not recognized', or 'Could not find or load main class'

Keeping a project folder tidy

As soon as you have more than one file, keep source files in `src` and compiled `.class` files in `out`. The `-d` option tells `javac` where to write `.class` files, and `-cp` (classpath) tells the JVM where to look for classes. Build tools such as Maven and Gradle do the same job on a larger scale.

bash
my-app/
  src/HelloWorld.java

javac -d out src/HelloWorld.java    # writes out/HelloWorld.class
java -cp out HelloWorld             # look for classes in out/

In real projects

How teams do it

On a team, 'it works on my machine' is not an acceptable answer. So the JDK version is written into the project itself: `<maven.compiler.release>21</maven.compiler.release>` in Maven, or a Java toolchain setting in Gradle. Developers often keep several JDKs installed and switch between them per project with a tool such as SDKMAN!.

The CI server has its setup written as code too. In GitHub Actions, `actions/setup-java` with `distribution: temurin` and `java-version: 21` installs the same JDK for every build, and the container image used for deployment matches that version. If you understand the terminal commands from this lesson, you can read the log when a build fails on one of those IDE-less servers and see what went wrong right away.

Common mistakes

Common setup mistakes

Typing `.class` in the `java` command

Type `java HelloWorld.class` and the JVM looks for a class literally named `HelloWorld.class`, failing with 'Could not find or load main class HelloWorld.class'. `java` takes a class name, not a file path, so type `java HelloWorld`.

A file name that does not match the public class

Write `public class HelloWorld` inside `hello.java` and `javac` refuses it. The `java hello.java` source launcher relaxes this rule, so it may seem to work, but once the file is in a project, build tools use `javac` and the build breaks. Keep the names identical from the start, including upper and lower case.

Fixing PATH but reusing an old terminal

After changing environment variables, terminals that are already open still see the old values. Open a new terminal before checking `javac -version` again.

Best practices

A checklist after every setup

  • In a new terminal, run `java -version` and `javac -version` and compare the version numbers
  • Open the JDK in your IDE's project settings and compare it with the version the terminal reports
  • Uninstall old JDKs you no longer need; when two JDKs are on PATH, the shell uses whichever it finds first
  • Write the required Java version and the compile and run commands in the project README; you may forget them yourself next month

Mini challenge

Mini challenge: run a Greeting class two ways

Write a `Greeting` class in `Greeting.java` that prints two lines: your name and that today's setup is done. Then run it once with `javac` + `java` and once with `java Greeting.java`, and compare what ends up in the folder.

  • The class name and file name must match exactly
  • Print two lines
  • After running `javac`, check that `Greeting.class` exists
  • Try `java Greeting.class` and read the error you get
Greeting.java
// TODO: declare a public class whose name matches this file
    // TODO: add the main method the JVM looks for
        // TODO: print "Hello from <your name>!"
        // TODO: print "My JDK setup works."

// Then, in a terminal:
// TODO: compile with javac, run with java, and try the java Greeting.java shortcut

Expected: Both ways print `Hello from Aung!` and `My JDK setup works.`. Only the `javac` route leaves `Greeting.class` behind, and `java Greeting.class` fails with 'Could not find or load main class'.

Show hint

After `javac Greeting.java`, type `java Greeting`. The shortcut mode does not leave a `.class` file behind.

Show solution
Greeting.java
public class Greeting {
    public static void main(String[] args) {
        System.out.println("Hello from Aung!");
        System.out.println("My JDK setup works.");
    }
}

// javac Greeting.java      -> creates Greeting.class
// java Greeting            -> runs it
// java Greeting.java       -> compiles in memory and runs, no .class file

Quick quiz 1 / 3

After `javac HelloWorld.java` succeeds, which command runs the program?

Quick quiz 2 / 3

Run works in the IDE, but the terminal says `'javac' is not recognized`. What is the most likely cause?

Quick quiz 3 / 3

After running `java HelloWorld.java` (the source launcher), what is in the folder?

What to remember

  • You need a JDK to write Java programs; choose an LTS version such as Java 21
  • In a fresh terminal, check that `java -version` and `javac -version` both work and agree
  • `javac File.java` writes a `.class` file and `java ClassName` runs it; `java File.java` is the single-file shortcut
  • Always keep the public class name and the file name identical

Java Language Changes, Java SE 25 — Oracle

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Get Started (Setup) | Thuta Learning