Every Java program that you will ever write needs two programs on your computer. The first one is a compiler, and it turns the source code that you type into a form that the computer is able to run. The second one is a runtime, and it runs that form for you. Java is kind to you here, because both of these programs arrive together in a single download. That download is called the JDK, and the Java Development Kit is the full name that you should look for.
Install the JDK and not only a runtime, because you need the compiler in order to build your own programs. A machine that can run Java programs but cannot compile them is a machine that can only use programs that somebody else has already built.
This is the first lesson in the module, and the next lesson in this module is about "What Does Your First Java Program Look Like and How Do You Run It?", which shows you the smallest program that actually does something.
What exactly are you installing?
The JDK is a folder full of programs that all work together. The most important two of them are the compiler and the runtime. The compiler is called javac, and it reads files that end in .java. The runtime is called java, and it reads files that end in .class.
Those two file types are the whole cycle. You write a file that ends in .java. The compiler reads it and writes a file that ends in .class. The runtime reads that file and runs your program.
The JDK also holds a few other tools that you will meet later in this course. The jshell program lets you try out small pieces of code without writing a whole class. The jar program packs your compiled files into a single archive that you can send to somebody else. The javadoc program writes documentation from the comments that you put in your code. The debugger lets you step through a program one line at a time and look at what is in memory.
JDK folder
|-- bin/ javac, java, jshell, jar, javadoc and the other tools
|-- lib/ the compiler's own libraries and the class library
|-- conf/ the settings that the tools read
|-- include/ the header files that native code needs
The bin folder is the important one for this lesson, because that is the folder that has to be on your PATH. The PATH is a list of folders that your operating system searches when you type a command. If the bin folder is not on that list, then typing javac gives you an error that says the command is not recognized.
How do you install it on your system?
There are several companies that build the JDK, and any of them will do for learning. The most common choices are Oracle, Eclipse Temurin and the Microsoft Build of OpenJDK. All of them are free to download, and all of them contain the same programs with slightly different packaging.
On Windows, you download an installer file and you run it. A window walks you through the steps. Accept the default choices all the way through, because the defaults are fine. When the installer finishes, the JDK is sitting in a folder that is usually called something like C:\Program Files\Java\jdk-21. The next step is to open a new terminal window, because the PATH change that the installer makes only reaches windows that you open afterwards.
On macOS, the easiest way is to use a package manager called Homebrew. You run one line in your terminal and the JDK is installed.
brew install openjdk
You can also download an installer file from a vendor and open it, which works the same way as on Windows. Homebrew is easier because it also keeps the package up to date for you later.
On Linux, you use the package manager that comes with your distribution. On Debian and Ubuntu that is apt, and the command looks like this.
sudo apt update
sudo apt install openjdk-21-jdk
Read that package name carefully, because it is the single most common mistake on Linux. The package that ends in -jdk contains the compiler. The package that ends in -jre contains only the runtime. If you install the -jre package, then java works and javac does not, and you will spend an afternoon wondering why.
Whatever system you use, try to keep only one version of the JDK installed at a time. Having several versions is allowed, but it makes it much harder to work out which one your tools are actually using.
How do you check that it worked?
Open a fresh terminal window and ask both tools to say their version.
java -version
javac -version
$ java -version
openjdk version "21.0.3" 2024-04-16
OpenJDK Runtime Environment (build 21.0.3+9)
OpenJDK 64-Bit Server VM (build 21.0.3+9, mixed mode)
$ javac -version
javac 21.0.3
Both commands must answer. If the first one answers and the second one gives you an error, then you have a runtime on your machine but no compiler, and you need to install the JDK properly.
It is also worth asking the operating system where it found each program. On Windows the command is where, and on macOS and Linux the command is which.
where java
where javac
C:\Program Files\Java\jdk-21\bin\java.exe
C:\Program Files\Java\jdk-21\bin\javac.exe
Both paths should point inside the same bin folder of the same JDK. When they point at different folders, you have two installations on your machine and the one that runs your program may not be the one that compiled it.
What usually goes wrong?
Three failures account for almost every problem that beginners meet on the first day.
The first one is a stale terminal. When you add a folder to your PATH, only the programs that start after that change can see it. So if you edit your PATH settings and then run a command in the window that was already open, the old PATH is still in effect. Close the window and open a new one. This catches everybody once.
The second one is a half installation. This is the case where a runtime is present but the compiler is not. You run java and it works, and you run javac and it says the command is not recognized. The fix is to install the JDK instead of the runtime, and to make sure that its bin folder comes before any other Java folder on your PATH.
The third one is a version mismatch. This happens when the java on your PATH is older than the javac that compiled your program. A program that was compiled against a newer version of the standard library will then fail while it is running, with an error about a missing class or a missing method. That error looks like a bug in your own code, but it is really a problem with the order of your PATH.
Two commands answer most of these mysteries. Printing the resolved paths shows you which copy of each tool your terminal is really using. And javac -verbose on a real source file shows you everything the compiler is doing, including which files it loaded.
javac -verbose Greeting.java
Do you need an editor or a full development tool?
No. Any plain text editor will do, and for the first few lessons of this course that is actually the better choice. When you type the commands yourself, you learn what the toolchain really does, because nothing is hidden from you.
A full development tool is excellent later on. It manages the classpath for you, it runs the tests, and it lets you step through an exception one line at a time. All of that is a real help once your project has many files. The only downside is that it hides the commands that this lesson is about.
So install the JDK now, keep a plain text editor open next to it, and add the bigger tool when your project outgrows a single file.
Quick reference
- The JDK contains the compiler, the runtime and the other developer tools. Install the JDK.
- Check the install with
java -versionandjavac -version. Both of them must answer. - If a command is not recognized, the bin folder of the JDK is missing from your PATH. Add it and then open a new terminal window.
- On Linux, install the package whose name ends in
-jdk, because the package that ends in-jrehas no compiler in it. - Keep one version of the JDK installed. Mixing several vendors is allowed, but it makes version problems harder to read.
Try it yourself: install the JDK, open a new terminal, and write down the output of java -version, javac -version and the command that prints the path of each tool. Then run your program from a folder where only the runtime exists, so that you can see the difference between the two tools for yourself.