How to Check Java Version Quickly and Easily
To check your Java version, open a terminal or command prompt and run
java -version. You can also check it programmatically in Java by calling System.getProperty("java.version").Syntax
Use the command java -version in your terminal or command prompt to see the installed Java version. In Java code, use System.getProperty("java.version") to get the version as a string.
java -version: Command line instruction to display Java version.System.getProperty("java.version"): Java method to retrieve version programmatically.
bash
java -version
Output
java version "17.0.5" 2022-10-18 LTS
Java(TM) SE Runtime Environment (build 17.0.5+8-LTS-622)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.5+8-LTS-622, mixed mode, sharing)
Example
This example shows how to print the Java version from a Java program using System.getProperty("java.version"). It helps you check the version inside your code.
java
public class CheckJavaVersion { public static void main(String[] args) { String version = System.getProperty("java.version"); System.out.println("Java version: " + version); } }
Output
Java version: 17.0.5
Common Pitfalls
Some common mistakes when checking Java version include:
- Running
java -versionin a terminal without Java installed or not in PATH, which causes an error. - Confusing
java -versionwithjavac -version(the latter shows the compiler version). - Using outdated Java versions without realizing it because multiple Java versions are installed.
Always ensure your terminal or command prompt recognizes the java command and check your system PATH settings.
java
/* Wrong way: Trying to print version with incorrect property name */ public class WrongVersionCheck { public static void main(String[] args) { // This will print null because the property name is wrong System.out.println(System.getProperty("java.version_wrong")); } } /* Right way: Use correct property name */ public class RightVersionCheck { public static void main(String[] args) { System.out.println(System.getProperty("java.version")); } }
Output
null
17.0.5
Quick Reference
Summary tips for checking Java version:
- Use
java -versionin terminal for quick check. - Use
System.getProperty("java.version")in Java code. - Check your PATH environment variable if
javacommand is not found. - Remember
javac -versionshows compiler version, not runtime.
Key Takeaways
Run
java -version in terminal to see your Java runtime version quickly.Use
System.getProperty("java.version") in Java code to get the version programmatically.Ensure Java is installed and your PATH includes the Java bin directory to avoid command errors.
Don't confuse
java -version (runtime) with javac -version (compiler).Use the exact property name "java.version" to get the correct version string in code.