Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know JDK version from within Java code

Tags:

How to know JDK version from within Java code

like image 648
Muhammad Hewedy Avatar asked Jan 09 '11 12:01

Muhammad Hewedy


People also ask

How can I tell what version of Java a program is using?

Type "java -version" into the Command Prompt, then press Enter on your keyboard. After a moment, your screen should display the information your computer has about Java, including what version you have installed.


2 Answers

I presume you mean just the Java version, in which case try this:

String version = System.getProperty("java.version"); 
like image 81
Bart Kiers Avatar answered Dec 04 '22 16:12

Bart Kiers


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.

like image 22
Thorbjørn Ravn Andersen Avatar answered Dec 04 '22 18:12

Thorbjørn Ravn Andersen