rahul
rahul

Reputation: 215

how to check jre version using java application

i have made an application in JDK7, but the jre6 is still using in the market, and if i send my jar file to someone with jre6, it wont work, is there a way that application checks the jre version and if it doesn't compat then ask user to update..

Upvotes: 11

Views: 18894

Answers (6)

ria
ria

Reputation: 7974

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.

Upvotes: 0

Ravi Jain
Ravi Jain

Reputation: 1482

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.

Upvotes: 2

Pau Kiat Wee
Pau Kiat Wee

Reputation: 9505

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.

Upvotes: 12

Tiago Peczenyj
Tiago Peczenyj

Reputation: 4623

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

Upvotes: 0

Sibster
Sibster

Reputation: 3189

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

Upvotes: 0

Jigar Joshi
Jigar Joshi

Reputation: 240900

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

Upvotes: 2

Related Questions