how to check jre version using java application

北战南征 提交于 2019-11-29 09:37:05

You can use System.getProperty(String key); method with "java.version" as key.

String version = System.getProperty("java.version");

Example output:

1.6.0_30

The available keys can find at here.

System.getProperty("java.version")

Note: It will return current jvm's version, jvm on which this code is executing, If you have java6 & java7 installed on your computer and you are running this code on java 7 it will show you version 7

static public void main(String[] arg) throws IOException
    {
        PrintStream out = System.out;
        Properties pro = System.getProperties();
        Set set = pro.entrySet();

       Iterator<Map.Entry<String , String >> itr = set.iterator();
        while(itr.hasNext())
        {
            Map.Entry ent = itr.next();
            out.println(ent.getKey() + " -> " + ent.getValue() + "\n");
        }
}

Use System.getProperty("java.version") or System.getProperty("java.runtime.version") to get installed version of java.
The above code snipped helps you find out more details such as java vendor name , OS etc.

here is an example that checks the version and trows an exception if the version is incorrect using System.getProperty("java.version");

Using Java Web Start you have a messagebox about the java version if it is not compatible with your jar. For example:

<resources>
       <j2se version="1.4+"
             href="http://java.sun.com/products/autodl/j2se" />

in the jnlp file

UnsupportedClassVersionError would occur if you try to run a Java file on an older version of jre compared to the version on which it was compiled.

java.lang.UnsupportedClassVersionError: Bad version number in .class file [at java.lang.ClassLoader.defineClass1(Native Method)] on running a compiled java class file.

In essence you can not run .class files that are compiled with a newer version than the JVM.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!