Thursday, 2 October 2014

Question

How do I determine the version of the Java Virtual Machine from an applet or application?

Answer

You can determine the JVM version and vendor by accessing system properties. If you're not familiar with Java Properties, a Properties object contains a mapping between string keys and string values. The JVM version is stored in the system properties object, accessible via the static System.getProperties() method. The Java API documentation lists the following keys and descriptions
KeyDescription of Associated Value
java.versionJava Runtime Environment version
java.vendorJava Runtime Environment vendor
java.vendor.urlJava vendor URL
java.homeJava installation directory
java.vm.specification.versionJava Virtual Machine specification version
java.vm.specification.vendorJava Virtual Machine specification vendor
java.vm.specification.nameJava Virtual Machine specification name
java.vm.versionJava Virtual Machine implementation version
java.vm.vendorJava Virtual Machine implementation vendor
java.vm.nameJava Virtual Machine implementation name
java.specification.versionJava Runtime Environment specification version
java.specification.vendorJava Runtime Environment specification vendor
java.specification.nameJava Runtime Environment specification name
java.class.versionJava class format version number
java.class.pathJava class path
os.nameOperating system name
os.archOperating system architecture
os.versionOperating system version
file.separatorFile separator ("/" on UNIX)
path.separatorPath separator (":" on UNIX)
line.separatorLine separator ("\n" on UNIX)
user.nameUser's account name
user.homeUser's home directory
user.dirUser's current working directory
Not every JVM is guaranteed to support these properties. For example, the Microsoft JVM will return null values for a request to get the 'java.vm.vendor' property. It will, however, return a value for 'java.vendor'. It may be better to check for these values instead, to guarantee compatibility. An example of this is as follows:-
import java.util.Properties;


public class jvm_version
{
 public static void main(String args[])
 {
  Properties prop = System.getProperties();
  System.out.println ("JVM Vendor : " + prop.getProperty("java.vendor") );
  System.out.println ("JVM Version: " + prop.getProperty("java.version") );
 }
}

Simple Program To Find Java Vendor Information

import java.util.Properties;
public class jvm_version
{
public static void main(String args[])
{
Properties prop = System.getProperties();
System.out.println ("JVM Vendor : " + prop.getProperty("java.vendor") );
System.out.println ("JVM Version: " + prop.getProperty("java.version") );
}
}

Note: class jvm_version is public, should be declared in a file named jvm_version.java