Java BasicsCore· 25 min read

Setting Up Java (JDK, javac, java)

Install the JDK, then compile with javac and run with java — your two everyday commands.

What you will learn

  • Install the JDK and check it works
  • Compile a file with javac
  • Run the program with java

Install the JDK

To write Java you need the JDK (Java Development Kit). It includes the compiler (javac) and the runner (java). Download it from adoptium.net (a free, popular source) or oracle.com/java, then install it like any program.

After installing, open a terminal (Command Prompt on Windows, Terminal on Mac) and check the version:

Check that Java is installed
java -version
javac -version

Note: Output: java version "21.0.2" javac 21.0.2 If you see version numbers like these, the JDK is installed and ready. The exact numbers will differ — any recent version is fine. If you see an error like command not found, the JDK is not installed or not on your PATH.

That last word, PATH, is a list your operating system keeps of the folders where it looks for commands. When the JDK installer adds Java to your PATH, you can type java from any folder and the computer knows where to find it. If java -version says command not found, it usually means the JDK is installed but not yet on the PATH — reinstalling and choosing the option to add Java to PATH normally fixes it.

Compile and run

Java code lives in a file ending in .java. Say you have a file called Hello.java. You compile it, then run it:

Two steps: compile, then run
javac Hello.java
java Hello

Note: Output: Hello, world! The first command (javac Hello.java) creates a file called Hello.class — that is the bytecode. The second command (java Hello) runs it. Notice you write java Hello with NO .java and NO .class on the end — just the name.

The whole journey, step by step

Here is the full path your code travels every time, from typing it to seeing it run:

  1. Write your code in a plain text file ending in .java, for example Hello.java.
  2. Compile it with javac Hello.java. This creates Hello.class — the bytecode the JVM understands.
  3. Run it with java Hello (just the name, no extension). The JVM reads Hello.class and your program runs.
  4. See the output in the terminal — here, Hello, world!.
  5. Change and repeat: edit the .java file, then compile and run again. You must re-compile after every change, or you will keep running the old version.

Watch out: A very common beginner trip-up: the file name must match the class name exactly, including capital letters. A class named Hello must be saved as Hello.java (not hello.java). We cover the class in the next lesson.

Tip: Many people use a friendly editor like VS Code or IntelliJ IDEA that can compile and run with one click. But knowing the two commands javac and java means you understand exactly what that click does.

Q. You wrote Hello.java. Which pair of commands compiles and then runs it?

Answer: First javac Hello.java compiles to bytecode (Hello.class), then java Hello runs it — using just the name, with no file extension.

✍️ Practice

  1. Install the JDK and run java -version to confirm it works.
  2. Write down, in your own words, what javac does and what java does.

🏠 Homework

  1. Take a screenshot of javac -version working on your computer, ready for your first program in the next lesson.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →