10

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..

rahul
  • 205
  • 3
  • 6
  • 12
  • Here is a [link](http://stackoverflow.com/questions/198431/how-do-you-compare-two-version-strings-in-java) how to compare the JRE version. – Crazenezz May 16 '12 at 07:05
  • @Crazenezz: is there a way by checking thru dos, by creating a windows batch file and virtual basic, could this be done?? if it match then it'll execute the jar?? can this be practically done wt i am saying?? – rahul May 16 '12 at 07:08
  • There is a possibility to do that in batch. You can convert the algorithm in the link that I give above and translate it with batch programming style (If you feel comfortable using that). – Crazenezz May 16 '12 at 07:22

6 Answers6

12

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.

Pau Kiat Wee
  • 9,485
  • 42
  • 40
  • will that work if jre is mismatching...cause in my case the jre file just gives the splash screen and closes itself if i use jre6.. i need some fool proof solution!! – rahul May 16 '12 at 06:57
  • its not showing any exception...the program wont run...i had made an exception error dialog box.....but nothing works.. – rahul May 16 '12 at 07:05
2
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

jmj
  • 237,923
  • 42
  • 401
  • 438
  • will that work if jre is mismatching...cause in my case the jre file just gives the splash screen and closes itself if i use jre6.. i need some fool proof solution!! – rahul May 16 '12 at 06:57
  • its not giving any exception....its just not running...i have made a error dialog in case of any exception....so i havent shown anything.... – rahul May 16 '12 at 07:14
2
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.

Ravi Jain
  • 1,452
  • 2
  • 17
  • 41
0

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

Sibster
  • 3,081
  • 21
  • 18
  • will that work if jre is mismatching...cause in my case the jre file just gives the splash screen and closes itself if i use jre6.. i need some fool proof solution!! – rahul May 16 '12 at 06:58
  • I cant verify it atm but if you put this in a class that gets loaded before any jre7 specific classes are loaded it should work – Sibster May 16 '12 at 07:05
0

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

Tiago Peczenyj
  • 4,387
  • 2
  • 22
  • 35
0

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.

ria
  • 7,904
  • 11
  • 39
  • 46