Sub topic 6
Check the Java and Compiler Versions
Before writing a Java program, check that your terminal can find both tools you need. The terminal is a window where you type commands. A command is an instruction you give your computer.
The Java Development Kit, or JDK, contains these tools:
javastarts the Java runtime, which runs Java programs.javacstarts the compiler, which changes Java code into a form the runtime can run.
You need both tools for the steps later in this tutorial. This chapter assumes you have installed a JDK and made its commands available in your terminal.
Run the version checks
Open a new terminal window. You can run these checks from any folder. You do not need to create a Java file first.
Enter these commands, pressing Enter after each one:
java -version
javac -versionThe -version part asks each tool to report its version. A version is a number that identifies a particular release of a tool.
The first command may show several lines. Its first line might look like this:
openjdk version "21.0.2" 2024-01-16The second command might show:
javac 21.0.2Your output will probably differ. It may include the name of the company that provided your JDK, a different date, or a different version number. That is normal.
Read the results
For versions 9 and later, the first number is the main version. In 21.0.2, the main version is 21. The other numbers identify updates within that version.
Some older Java versions use a different pattern. For example, 1.8.0_402 means Java 8, not Java 1.
Compare the two results:
| What you see | What it means |
|---|---|
| Both commands report the JDK version you installed | The terminal can find both tools. |
| `java` reports a version, but `javac` is not found | The runtime is available, but the compiler is not available through this terminal. |
| Both report versions, but their main numbers differ | The commands may be coming from different Java installations. |
| Neither command is found | The terminal cannot find either tool. |
For a simple setup, use the same main version for both commands. For example, java version 21 and javac version 21 are a good match. The full numbers do not have to be identical for these checks to work, but matching tools from the same JDK make it easier to avoid confusion.
Different main versions are not always wrong. However, they can cause problems. For example, a newer compiler may produce a program that an older runtime cannot run. If the numbers differ and you did not intend that setup, check which JDK each command uses.
If a command is not found
First, check the spelling and the space before -version. Then close the terminal, open a new one, and try both commands again. A terminal that was already open may not have picked up a recent setup change.
If javac still is not found, make sure you installed a JDK, not just a runtime. The runtime can run Java programs, but it does not include the compiler. Also check that the JDK’s bin folder was added to your system’s PATH. PATH is the list of folders your system searches for commands.
If you need to change that setting, return to the chapter on making Java commands available. Then open a new terminal and repeat both version checks.
Check where the commands come from
If you have more than one Java installation, you can ask the terminal where it finds the commands. This can help explain a version mismatch.
On Windows, enter:
where.exe java
where.exe javacOn macOS or Linux, enter:
command -v java
command -v javacThese commands show paths: locations on your computer. On Windows, you may see more than one location. On macOS or Linux, a result may point to a launcher or a link rather than directly to the JDK folder.
Use the locations as clues, then check your PATH setup if needed. Run the version checks again after making a change. The goal is to see both tools report the versions you expect before you move on.
Practice
Exercise
java -version reports version 17, but javac -version reports version 21. What should you check?
Show answer
Check where each command comes from and review your PATH. They may be using different Java installations. For a simple setup, choose tools with the same main version, then run both version checks again.