how to check jre version using java application

前端 未结 6 927
死守一世寂寞
死守一世寂寞 2020-12-19 01:49

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

相关标签:
6条回答
  • 2020-12-19 02:00

    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

    0 讨论(0)
  • 2020-12-19 02:02

    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.

    0 讨论(0)
  • 2020-12-19 02:03
    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.

    0 讨论(0)
  • 2020-12-19 02:03

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

    0 讨论(0)
  • 2020-12-19 02:08

    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.

    0 讨论(0)
  • 2020-12-19 02:17
    System.getProperty("java.version")
    
    • Demo

    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

    0 讨论(0)
提交回复
热议问题