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.
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.
- Install the JDK
- PATH lets the shell find javac
- javac writes the .class file
- 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
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.
// 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 HelloWorldCode breakdown
- The file name is part of the contract
// Save this file as HelloWorld.javaA 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'.
- The class the JVM will load
public 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.
- Step 1: compile
// javac HelloWorld.javaChecks the source file and writes `HelloWorld.class` containing bytecode. No output means success; you only see messages when something is wrong.
- Step 2: run
// java HelloWorldStarts the JVM, loads the `HelloWorld` class and calls `main`. Type only the class name here, with no `.java` or `.class` extension.
Compiled and running from terminal!- Save HelloWorld.java
- javac checks it and writes HelloWorld.class
- java HelloWorld starts the JVM
- 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.
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
// 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 shortcutExpected: 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
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 fileQuick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
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