The quickest way to check your Java version from the command line is to type java -version and press Enter. If the command is recognized, the terminal window will display the version number and vendor information. If Java is not installed or not added to your system’s PATH environment variable, the terminal will return an error message like "command not found." This guide covers the exact steps to run this command, explains what the output means, and provides troubleshooting steps for common issues where the command fails or displays the wrong version.
What You'll Need
- A terminal or command prompt window. On Windows, this is the Command Prompt or PowerShell. On macOS or Linux, it is the Terminal application.
- A working Java installation. The command
java -versionworks only if Java is installed on the system and accessible to the command line environment. - Administrator privileges (optional). While checking the version does not strictly require them, installing or updating Java often does.
Step-by-Step
- Open your terminal application. Locate the terminal on your computer. On Windows 10 and 11, you can search for "Command Prompt" or "PowerShell" in the Start menu. On macOS, press Command + Space, type "Terminal," and open the application found in the Utilities folder. On Linux distributions, you can typically find it in your application menu or by pressing Ctrl + Alt + T.
- Verify the command exists. In the terminal window, type the command
javafollowed by a space and then the flag-version. Ensure there is a space between the command and the flag. The exact text should look like this:java -version. - Press Enter to execute. Press the Enter key on your keyboard to send the command. The terminal will process the request and display the output.
- Read the output. The terminal will display one or more lines of text. The first line usually contains the version information. You are looking for the string that begins with
java version. - Check for JDK vs. JRE. The output will tell you if you are running the Java Runtime Environment (JRE) or the Java Development Kit (JDK). If the output contains the word "Development Kit" or "JDK," you have the development tools installed. If it mentions "Runtime Environment" or "JRE," you have the tools needed to run Java applications but not to compile them.
- Verify the vendor (optional). The output often includes the vendor name, such as "Oracle Corporation" or "Amazon.com Inc. (Corretto)." This confirms that the Java installation is active and not a substitute or a generic stub.
- Check for multiple versions (Advanced). If you have multiple versions of Java installed (common on Windows), the simple
java -versioncommand might display the version of the first executable found in your PATH. To check the specific path of the current Java installation, you can use thewhere javacommand on Windows or thewhich javacommand on macOS and Linux. This helps identify which specific installation is being called.
Common Mistakes
One of the most frequent errors when using the terminal to check Java is confusing the command for the Java compiler with the command for the Java runtime. The command java is used to execute Java programs (the runtime). The command javac is used to compile Java source code (the compiler). If you type javac -version and get an error, it does not mean Java is missing; it simply means the compiler tool is not installed or not in your PATH, even if the runtime is present.
Another common mistake is failing to restart the terminal after installing Java. On Windows, new environment variables added during installation are not immediately available in a currently open command prompt window. You must close the Command Prompt or PowerShell window completely and reopen it for the system to recognize the new path to the Java executable.
Users often overlook the distinction between the system-wide Java installation and the Java version inside a specific project folder. If you are working in a development environment like IntelliJ IDEA or Eclipse, the project may be configured to use a specific JDK located in its own folder, overriding the system default. In this case, the terminal command java -version will still report the system default, not the project’s specific version.
Tips
For a cleaner output, you can combine the version check with the command to find the location of the Java executable. On Windows, you can type java -version && where java. The ampersand symbol && tells the terminal to run the second command only if the first one succeeds. This provides both the version number and the full file path to the executable, which is very useful for troubleshooting.
On Linux and macOS, the javac command is a reliable way to confirm that you have both the runtime and the compiler installed. If the javac command is recognized and returns a version number, it confirms that the JDK is correctly installed. This is often a better test for developers than just checking the runtime version.
When checking output from a script or a CI/CD pipeline, the version string may be long and spans multiple lines. It is good practice to capture the output into a variable or redirect it to a file to inspect the full details. For example, typing java -version 2>&1 | tee java_output.txt will display the output in the terminal and save it to a text file named java_output.txt in the current directory.
Troubleshooting
If the command returns "command not found" or "'java' is not recognized as an internal or external command," Java is not installed on your system. You must download and install the Java Runtime Environment (JRE) or Java Development Kit (JDK) from the official website of the software vendor (such as Oracle or Amazon). After installation, you must restart your computer or the terminal for the system to find the new program.
If the command runs but displays an error message or does not show a version number, the Java executable might be corrupted or incomplete. This can sometimes happen if an installation was interrupted. The best solution is to completely uninstall the current Java installation (using the "Add or Remove Programs" tool on Windows or the package manager on Linux) and then download and install a fresh copy from the official source.
If the command shows a version number, but you cannot run your Java application, the issue is likely with your application’s configuration or code. The version you see in the terminal is the system-wide version. Your application might require a specific version of Java that is not compatible with the one installed on your system. You can check the application’s documentation to see if it specifies a required Java version.
If you are on Windows and have multiple versions of Java installed, the java -version command might not always point to the version you expect. To change the default Java version, you need to modify the PATH environment variable in the Windows System settings. This involves moving the path to the correct Java executable to the top of the list of directories that the system searches for programs.
Frequently Asked Questions
What is the difference between java -version and javac -version?
java -version checks the version of the Java Runtime Environment (JRE), which is used to run Java applications. javac -version checks the version of the Java Compiler (JDK), which is used to create and compile Java source code. You can have one without the other.
Why does the terminal show a different version than my code editor?
Your code editor (like IntelliJ or Eclipse) often allows you to configure a specific Java Development Kit (JDK) for each project. The terminal uses the system-wide JDK, which might be different from the project-specific JDK configured in your editor.
How do I know if I have 32-bit or 64-bit Java installed?
The simplest way is to check the output of the java -version command. While it does not always explicitly state the bitness, you can cross-reference this with the output of the where java (Windows) or which java (macOS/Linux) command. The path usually contains "/x64/" for 64-bit installations or "/i586/" for 32-bit installations.
Does the version number tell me if I have the latest Java?
The version number itself does not indicate if it is the latest. You must compare the major and minor version numbers shown in the output with the latest release listed on the official Java download website. For example, a version of 1.8.0_311 is an older release, while 17.0.1 is a more recent release.
Can I have multiple Java versions on my computer?
Yes, it is common to have multiple versions of Java installed on a Windows computer. Different applications may require different versions. However, only one version can be the default, and the java -version command will always report the default version unless you manually specify the path to a different executable.
The command works in PowerShell but not in Command Prompt. Why?
This is a known issue. PowerShell has a built-in alias for the word "java" that points to the Windows Management Instrumentation (WMI) command, which can interfere with the Java executable. You can disable this alias by typing javaws (the Java Web Start launcher) in PowerShell, or simply use the full path to the java.exe file to bypass the alias.