Sub topic 6
Check the Java and Compiler Versions
You have installed a Java Development Kit, or JDK, and made its commands available in your terminal. Now you will check that the terminal can find and start the tools you need.
A terminal is a window where you type commands. A command tells the computer to perform an action. The terminal shows the result as output, which is text printed in the window.
You will check two commands:
javastarts the Java runtime, the software that runs compiled Java programs.javacstarts the Java compiler, the tool that turns Java source code into a form the runtime can run.
Checking both matters. The java command may work even when javac is missing or comes from a different JDK.
Open a fresh terminal
Open the terminal you used during setup. If you changed your command search settings while that terminal was open, close it and open a new one first. A new terminal can pick up the updated settings.
The place where you type is called the prompt. It may show your name, your computer’s name, or the current folder. Its exact appearance varies.
You can run the version checks from any folder. You do not need a Java program file yet.
Type the commands in this chapter exactly as shown, then press Enter after each one. Do not type any prompt text that appears before the place where you enter the command.
Check the Java runtime
Enter this command:
java -versionThe -version part is an option: an extra instruction that asks the command to report its version. A version is a number that identifies a release of software.
Here is one example of the output:
openjdk version "21.0.2" 2024-01-16
OpenJDK Runtime Environment Temurin-21.0.2+13 (build 21.0.2+13-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.2+13 (build 21.0.2+13-LTS, mixed mode, sharing)Your output will probably differ. It may have a different version number or show a different JDK provider. For this check, focus on the first line.
In the example, 21.0.2 is the Java version. The first number, 21, is the main release number. You may also see it called the major version. The other numbers provide more detail about the release.
The output can also include:
- A provider name, such as
Temurin. - A release date.
- Details about the Java runtime.
- The letters
LTS, if the provider labels that release for long-term support.
You do not need to understand every word in the output. The important result is that the command started and reported a Java version.
Check the compiler
Now enter the second command:
javac -versionNotice the c at the end of javac. It makes this a different command from java.
A successful result may look like this:
javac 21.0.2The compiler’s output is usually shorter than the runtime’s output. In this example, the compiler is also from Java 21.
Run this check even if java -version worked. The first command confirms that the Java runtime can start. It does not confirm that the compiler is available.
Compare the results
Look at the version reported by each command. For a straightforward setup, the runtime and compiler should come from the same JDK installation. Their full version numbers will normally match.
| Runtime result | Compiler result | What to check |
|---|---|---|
| `21.0.2` | `21.0.2` | Both report the same release. That is the expected result for one JDK installation. |
| `21.0.2` | `21.0.1` | Both are Java 21, but the update numbers differ. Check which installations the commands are using. |
| `17.0.10` | `21.0.2` | The main versions differ. Check which installations the commands are using. |
| A version appears | No compiler version appears | The runtime works, but the compiler has not been verified. |
| No version appears | No version appears | Neither tool has been verified in this terminal. |
Different version numbers do not always mean that Java programs will fail. For example, a newer runtime can often run output made by an older compiler. But a program made by a newer compiler may not run on an older runtime.
For a beginner setup, using both tools from the same JDK avoids that uncertainty. If the numbers differ, note both results and check their locations before continuing.
An older version number
Older Java releases can use a different style of number. For example:
java version "1.8.0_402"This means Java 8, not Java 1. The matching compiler might report:
javac 1.8.0_402For versions such as 17.0.10 and 21.0.2, use the first number as the main version. For a version starting with 1.8, the main version is 8.
Check where the commands are found
A computer can have more than one Java installation. Even if both version commands work, your terminal might find java in one installation and javac in another.
The terminal uses a search setting called PATH to find commands. PATH is a list of folders to search when you enter a command name. You do not need to change it for this check. You can first ask where the commands are found.
On Windows
In Command Prompt or PowerShell, run:
where.exe java
where.exe javacA result might show paths like these:
C:\Program Files\Example JDK\bin\java.exe
C:\Program Files\Example JDK\bin\javac.exeA path is the location of a file or folder. In this example, both programs appear in the same bin folder of the same JDK. A bin folder commonly holds programs you can run as commands.
You may see more than one path for a command. That can mean more than one copy is available to search. The list is a useful clue, but it does not always prove which command a shell chooses in every situation.
Use where.exe, including .exe, as shown. In PowerShell, typing only where can refer to a different command.
On macOS or Linux
In a terminal, run:
command -v java
command -v javacAn example result is:
/usr/bin/java
/usr/bin/javacThese commands ask the shell, the program that reads your terminal commands, how it finds each name. Usually the result is a path. It can instead identify a shell shortcut or function, which is another kind of command definition.
On macOS, paths such as /usr/bin/java can point to system launchers rather than directly to the JDK folder. A launcher is a small program that starts another program. Therefore, a path in /usr/bin does not by itself tell you the JDK’s actual folder.
Location checks provide clues. The version commands show what starts when you enter java and javac. If you need to investigate a mismatch, keep both the version output and the location output.
Understand common results
Both commands report the expected version
If both commands report the version you intended to use, and their full version numbers match, you have confirmed that the terminal can start the runtime and compiler.
This is an important check, but it is not a test of a complete Java program. You will check compiling and running a program in a later chapter.
A command is not found
You may see a message such as “command not found” or “not recognized as an internal or external command.” The exact wording depends on your system and terminal.
This means the terminal could not find that command by name. Check your spelling first:
- Use
java -versionfor the runtime. - Use
javac -versionfor the compiler. - Include the hyphen before
version.
If you recently changed PATH, try the check in a newly opened terminal. If the command is still not found, return to the installation and command-availability steps before continuing.
`java` works, but `javac` does not
This result confirms only that the runtime command is available. The compiler may not be installed, or the terminal may not be able to find it.
Check that you installed a JDK, rather than only a runtime, and review how its commands were made available. Then open a fresh terminal and run both version checks again.
Both work, but the versions differ
Write down the two version numbers. Then use the location checks for your operating system to look for signs of different installations.
For example, if java reports Java 17 and javac reports Java 21, both commands are working, but they are not reporting the same main version. Review your JDK selection and command search settings so that both commands use the intended JDK.
Record your check
Before moving on, make a short note of:
- The version reported by
java -version. - The version reported by
javac -version. - Whether those versions match the JDK you intended to use.
- Any location output you may need if the versions differ.
If both commands start and report the expected version, you are ready for the later step of compiling and running a Java program.
Exercises
Exercise
Your terminal reports openjdk version "21.0.2" for java -version and javac 21.0.2 for javac -version. What have you checked?
Show answer
The terminal can start both the Java runtime and the compiler, and both report version 21.0.2. This does not yet test compiling and running a program.
Exercise
java -version reports a version, but javac -version says the command was not found. Can you conclude that the compiler is ready?
Show answer
No. Only the runtime has been verified. Check that a JDK is installed and that the terminal can find its compiler, then try both commands again.
Exercise
The runtime reports version 17.0.10, while the compiler reports 21.0.2. What should you do before relying on this setup?
Show answer
Note the different versions and check where each command is found. Review the selected JDK and command search settings so that both tools use the intended installation.