How to Check Your Java Version and Locate Your Java Runtime

Sometimes it is important to check your Java version and the path of your Java Runtime. You might have different Java versions on your computer and your program might not behave like it should.

Here is how you can check, which Java Runtime (JRE) is executing your program:

  • You can check your Java version with the Java system property “java.version”.
  • The Java system property “java.home” can tell you, exactly which Java Runtime Environment is executing your program.

Understanding Java System Properties

The Java class “System” has a property list that can tell you technical details about its environment. These system properties are key-value pairs that provide information e.g. about the current Java runtime environment, the operating system, and the Java virtual machine, its version, the Java library path.

key-value pairs mean: each one has a name and a value.

  • You can use System.getProperty(<propertyname>) to get the value of one property.
    • You can use System.getProperties() to get a list of all system properties.

    How to check your Java version

    This code returns the version of the JRE that is executing your program

    public class CheckJavaVersion {
        public static void main(String[] args) {
            String version = System.getProperty("java.version");
            System.out.println("Java version: " + version);
        }
    }

    The output might look like this:

    Java version: 1.8.0_74
    

    The string contains several numbers. Here

    • 1.8 is the major version
    • 0 is the minor version
    • 74 is the patch level

    There are two different formats for the version string. One is used up to Java 1.8, the other is used from Java 9 or higher, where they dropped the leading “1.”. Here are some examples:

    • Java 8 or lower: 1.6.0_23, 1.7.0, 1.7.0_80, 1.8.0_211
    • Java 9 or higher: 9.0.1, 11.0.4, 12, 12.0.1

    So if you run your program with different JREs, you can get different results. Try, for example, to switch the JRE in your Java IDE (like e.g. Eclipse) and you can see that the result of this program changes.

    If you have at least Java 9, you can split the string easily like this:

    public class CheckJavaVersion {
        public static void main(String[] args) {
            // Get the java.version property
            String version = System.getProperty("java.version");
            System.out.println("Java Version: " + version);
    
            // Use a regular expression to extract the major, minor, and patch components
            String[] components = version.split("[^\\d]+");
            int major = Integer.parseInt(components[0]);
            int minor = Integer.parseInt(components[1]);
            int patch = Integer.parseInt(components[2]);
    
            // Print the major, minor, and patch versions to the console
            System.out.println("Major Version: " + major);
            System.out.println("Minor Version: " + minor);
            System.out.println("Patch Version: " + patch);
        }
    }

    In my case (now running with a newer Java JRE than in the example above), I get

    Java Version: 18.0.1
    Major Version: 18
    Minor Version: 0
    Patch Version: 1
    

    How to locate Your Java runtime

    Sometimes you might want to know exactly which Java JRE is executing your program. You can check this using another one of the System properties, similarly to the Java version above.

    public class CheckJREPath {
        public static void main(String[] args) {
            String javaHome = System.getProperty("java.home");
            System.out.println("JRE path: " + javaHome);
        }
    }

    If you didn’t exactly specify your JRE when you called your program, you might be surprised to see which JRE you get here.

    In my case it was:

    JRE path: D:\install\jdk-18.0.1
    

    The exact path in your case may vary depending on your system configuration and operating system.

    How to list all Java System properties

    So what else can the system properties tell us? Let’s find out and list them all with this code:

    Let us call the System.getProperties() method to obtain a Properties object. This is a container of all the system properties. Using the entrySet() method, we get a list of all the keys. Then we loop over the keys and get their values.

    import java.util.Map;
    import java.util.Properties;
    import java.util.Set;
    
    public class ListAllSystemProperties {
        public static void main(String[] args) {
            Properties properties = System.getProperties();
            Set<Map.Entry<Object, Object>> entrySet = properties.entrySet();
            for (Map.Entry<Object, Object> entry : entrySet) {
                System.out.println(entry.getKey() + " => " + entry.getValue());
            }
        }
    }

    In my case, I get more than 50 entries.

    Here are some system properties that might be useful:

    • os.name: This returns the name of the operating system on which your Java virtual machine is running. In my case, it is “Windows 10”. But it might also be “Linux” or “Mac OS X”.
    • os.version: This returns the version of the operating system on which your Java virtual machine is running. In my case, it is 10.0, as I am running Windows 10.
    • os.arch: This returns the architecture of the operating system on which the Java virtual machine is running. In my case, it is “amd64”.
    • user.home: This is the home directory of the user that is running the program.
    • user.language: This is the language of the user. In my case, it is “de” for German.
    • user.dir: This returns the current working directory of the Java process.
    • file.separator: This one might be important. It returns the file separator character used by the operating system. Unix and Mac use “/” to separate the parts (folderd) in the directory name. Windows is the odd one out and uses “\”.
      Here is a Windows example: “c:\temp\somefile.txt”. If you need to assemble path names within Java, you need to use the right one. If your program needs to work on a Windows system as well as on a Linux system, you need to take care to switch depending on your platform.
    • line.separator: This one is similar. In a text filee, a Unix system will separate lines using “\n”, but Windows uses “\r\n”.
    • path.separator: (do not confuse this with the file separator). The path separator is used, when a String contains more than one file. A common example is the environment valiable PATH that lists all the directories where the operating system looks for a program to execute. Windows uses “;” and Unix uses “:” to separate the entries. In Windows you could see an example for such a path like this: open the Windows Command Shell (“cmd”) and type “path”. You will get a long list of directories, all separated by “;”.

    More java stuff can be found hereOpens in a new tab..

    Recent Posts