How to check jre version using java application

I made the application in JDK7, but jre6 is still using the market, and if I send the jar file to someone with jre6 it won’t work, is there a way the application checks the version of jre and if it is not compatible then ask the user update.

+7
source share
6 answers

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

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

Output Example:

 1.6.0_30 

Available keys can be found in here .

+9
source
 System.getProperty("java.version") 

Note It will return the current version of jvm, jvm on which this code is running. If java6 and java7 are installed on your computer and you run this code on java 7, it will show you version 7

+2
source
 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 the installed version of java.
The above code allows you to find out more details such as the name of the Java provider, OS, etc.

+2
source

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

0
source

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

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

in jnlp file

0
source

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

java.lang.UnsupportedClassVersionError: invalid version number in .class file [at java.lang.ClassLoader.defineClass1 (native method)] when running a compiled Java class file.

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

0
source

All Articles