6
$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.

flavio.donze
  • 7,432
  • 9
  • 58
  • 91
harunurhan
  • 1,516
  • 1
  • 17
  • 21

2 Answers2

13

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

flavio.donze
  • 7,432
  • 9
  • 58
  • 91
1

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

bruno
  • 2,213
  • 1
  • 19
  • 31