How to know JDK version from within Java code
Asked
Active
Viewed 2.7k times
4 Answers
50
I presume you mean just the Java version, in which case try this:
String version = System.getProperty("java.version");

Bart Kiers
- 166,582
- 36
- 299
- 288
4
Relying on the java.version string for anything else than showing to a human is fragile and will break if running on another Java implementation.
The only reliable way programatically is to use reflection to carefully ask if a given facility is available, and then select the appropriate code accordingly.

Thorbjørn Ravn Andersen
- 73,784
- 33
- 194
- 347
-
1Can you give an example of when this would fail? – Jesse Barnum Feb 26 '11 at 13:19
-
@Jesse, I'd rather put it the other way around. What rule would you set up for analyzing the java.version string? – Thorbjørn Ravn Andersen Feb 26 '11 at 14:21
-
Well, I would use java.specification.version instead of java.version, and the code would look like: If( Float.valueOf( System.getProperty("java.specification.version")) ≥= 1.6 ) {doJava6Thing}; else { doJava5Thing } – Jesse Barnum Feb 26 '11 at 19:41
-
1@Jesse, I was unaware of this property. I can, however, not see from a quick look in the JLS that it is guaranteed to be present and containing a valid number. Perhaps you know where it says so? – Thorbjørn Ravn Andersen Feb 26 '11 at 20:10
-
@Jesse, also please note that by using _another_ property you agree with that it is not java.version that is the property to be analyzed. – Thorbjørn Ravn Andersen Feb 26 '11 at 20:11
-
Yes, you're correct that java.version is not the best way to determine the JDK version. The java.specification.version is documented here: http://download.oracle.com/javase/1.4.2/docs/guide/versioning/spec/versioning2.html – Jesse Barnum Feb 27 '11 at 18:51
-
1@Jesse, the crucial question here if this is for Hotspot-based JVM's only. I cannot immediately tell. – Thorbjørn Ravn Andersen Feb 27 '11 at 20:07
-
2Don't use `Float.valueOf` like @JesseBarnum. This failed for me because `1.8>=1.8` returned false for me, due to round-off errors. Use `.compareTo("1.8")>=0` instead. – Mark Jeronimus Feb 05 '15 at 08:42
-
@MarkJeronimus, I think your way would work, but I'd feel safer with this check: new BigDecimal( specVersion ).compareTo( new BigDecimal( "1.8" ) ) >= 0; – Jesse Barnum Feb 05 '15 at 13:50
-
I recommend against `.compareTo("1.8") >= 0` because it will fail if there's a version `11.8` or `10.1.8`. If you're going to use strings, you need to use something like `startsWith` or a regular expression. – Sarah G Mar 05 '19 at 01:42
-
As of Java 9, the java.specification.version has moved to be a single integer. – Thorbjørn Ravn Andersen Mar 05 '19 at 09:41
-
sometimes one need version just to show in stdout or in logs - and nowhere logic relies on it's value – shabunc May 16 '19 at 09:19
-
@shabunc Then you are not "Relying on the java.version string for anything else than showing to a human". – Thorbjørn Ravn Andersen May 16 '19 at 16:27
4
Since java 9
System.out.println(Runtime.version());
Prints something like 17.0.2+8-86
or if you need more
System.out.println("Java Feature Version = " + Runtime.version().feature());
System.out.println("Java Interim Version = " + Runtime.version().interim());
System.out.println("Java Update Version = " + Runtime.version().update());
System.out.println("Java Version Build = " + Runtime.version().build().map(String::valueOf).orElse("Unknown"));
System.out.println("Java Version Pre-Release Info = " + Runtime.version().pre().orElse("N/A"));
Prints
Java Feature Version = 17
Java Interim Version = 0
Java Update Version = 2
Java Version Build = 8
Java Version Pre-Release Info = N/A

apflieger
- 912
- 10
- 18
0
If you need to know the bit version(32bit or 64 bit) as well, and you are running a hotSpot JVM, you can try this code:
System.getProperty("sun.arch.data.model")
No grantee that it will work for other Java implementaions rather than hotSpot.

ZhaoGang
- 4,491
- 1
- 27
- 39