Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get java version programmatically with update information

$java -version

prints like below

java version "1.8.0_40"
Java(TM) SE Runtime Environment (build 1.8.0_40-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.40-b25, mixed mode)

The info which I am interested in is complete java version, System.getProperties() doesn't seem to have that information. It only has the version before _, not update information which is 40 in my case.

EDIT: When I run the a test code System.out.println(System.getProperty("java.version"); prints the complete version. But I am working on a large codebase in which I can't get the same result. I checked the code-base if the property somehow overwritten at some point, but couldn't find anything.

EDIT_2: I figured out that java.version prints 1.8.0_40 when I manually compile the code with javac then run it with java on the terminal. However it prints only 1.8.0 when I run it via IntelliJ.

like image 261
harunurhan Avatar asked Apr 29 '16 09:04

harunurhan


2 Answers

You can get the complete version using this property:

java.runtime.version

This is an example:

public class Test {

    public static void main(String[] args) {
        System.out.println(System.getProperty("java.runtime.version"));
    }
}

On my machine I get the following output:

1.8.0_92-b14

If you only want the part before -, use the following system property:

java.version

This will output:

1.8.0_92

like image 103
flavio.donze Avatar answered Oct 16 '22 08:10

flavio.donze


enter image description here

Use these properties with below code

String version = System.getProperty("java.version");
        System.out.println("version : "+version);

String vendor = System.getProperty("java.vendor");
        System.out.println("java Vendor : "+vendor);

String vm = System.getProperty("java.vm.name");
        System.out.println("java vm name : "+vm);

Output :

version : 15

java Vendor : Oracle Corporation

java vm name : OpenJDK 64-Bit Server VM

=======

for more command see attached image

like image 1
Adarsh Shukla Avatar answered Oct 16 '22 09:10

Adarsh Shukla