Getting Java Version at Runtime

Getting Java version at runtime

These articles seem to suggest that checking for 1.5 or 1.6 prefix should work, as it follows proper version naming convention.

Sun Technical Articles

  • J2SE SDK/JRE Version String Naming Convention
  • Version 1.5.0 or 5.0?

    • "J2SE also keeps the version number 1.5.0 (or 1.5) in some places that are visible only to developers, or where the version number is parsed by programs"

      • "java.version system property"
  • Version 1.6.0 Used by Developers

    • "Java SE keeps the version number 1.6.0 (or 1.6) in some places that are visible only to developers, or where the version number is parsed by programs."

      • "java.version system property"

How to find the JVM version from a program?

System.getProperty("java.version") returns what you need.

You can also use JMX if you want:

ManagementFactory.getRuntimeMXBean().getVmVersion()

Determine java version used to call a program

You should use System.getProperty("java.version"). This will give you the version of the currently running JVM as a String, And you can then check for a prefix like 1.5 or 1.6, and you have the version of Java.

Also check this question, and the docs for System.getProperty(...).

Hope this helps.

Get Java Version using `Runtime.getRuntime().exec(commands)`

You don't get the version in your code because:
java -version
prints to the error stream, rather than stdout, I don't know why.

You can show this with:

java -version > output.txt

and see that it's still printed to your console, and nothing is in output.txt.

Or with:

java -version 2> error.txt

and see that nothing is printed and the version information is in error.txt

The question as to why it happens was asked here: Why does 'java -version' go to stderr?

What is the difference in properties java.runtime.version and java.version

System Property Name | System Property Content  | Where Displayed in java version Output
---------------------|--------------------------|---------------------------------------
java.version | product version | Line one displays the product version
---------------------|--------------------------|---------------------------------------
java.runtime.version | product version | Line one displays the product version
| product build identifier | Line two displays the build identifier

From the J2SE SDK/JRE Version String Naming Convention documentation:

  • The content of the java.runtime.version system property can be expanded (beyond that of the java.version system property) to include the build id.

It seems that the property value can therefore be equal to the java.runtime content or differentiate by the build id as already pointed out in the question.

Anyway, as previously stated in a comment to the question, the java.runtime.version property doesn't appear among the currently documented system properties.

How to get Java version of any given java path programmatically?

Okay after reading your comments I have wrote code that executes java -version

ProcessBuilder pb = new ProcessBuilder("java", "-version");
pb.directory(new File(System.getProperty("user.home")));
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String javaVersion = reader.readLine();
System.out.println(javaVersion.substring(javaVersion.indexOf("\"") + 1, javaVersion.lastIndexOf("\"")));

You can also call it with the EXACT path

ProcessBuilder pb = new ProcessBuilder("C:\\Program Files\\Java\\jdk1.8.0_181\\bin\\java", "-version");

this will output the java version as 1.8.0_181

Thanks to Andreas Hauschild that pointed out release file contains the version
I wrote code to parse the version from that file so use this. If you don't wanna execute commands and want to get the java version from release file I wrote a code to just parse the version from it

public static String getJavaVersion(String javaPath) throws IOException {
File releaseFile = new File(javaPath, "release");
BufferedReader bufferedReader = new BufferedReader(new FileReader(releaseFile));
String version = bufferedReader.readLine();
return version.substring(version.indexOf("\"") + 1, version.lastIndexOf("\""));
}

You can call it by using

String version = getJavaVersion("C:\\Program Files\\Java\\jdk1.8.0_181");


Related Topics



Leave a reply



Submit