java 如何使用java应用程序检查jre版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10613359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:49:28  来源:igfitidea点击:

how to check jre version using java application

compatibilityjava

提问by rahul

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

我已经在JDK7中创建了一个应用程序,但市场上仍在使用jre6,如果我将我的jar文件发送给使用jre6的人,它将无法工作,有没有办法让应用程序检查jre版本,如果没有compat 然后要求用户更新..

回答by Pau Kiat Wee

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

您可以使用System.getProperty(String key); "java.version"as 键的方法。

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

Example output:

示例输出:

1.6.0_30

The available keys can find at here.

可用的密钥可以在这里找到。

回答by Ravi Jain

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.

使用System.getProperty("java.version")System.getProperty("java.runtime.version")获取已安装的 java 版本。
上面截取的代码可以帮助您找到更多详细信息,例如 java 供应商名称、操作系统等。

回答by Jigar Joshi

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

注意:它将返回当前的 jvm 版本,此代码正在执行的 jvm,如果您的计算机上安装了 java6 和 java7,并且您在 java 7 上运行此代码,它将显示版本 7

回答by Sibster

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

这是一个检查版本并在版本不正确时抛出异常的示例,使用System.getProperty("java.version");

回答by Tiago Peczenyj

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

使用 Java Web Start,如果 Java 版本与您的 jar 不兼容,您会收到一个关于 Java 版本的消息框。例如:

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

in the jnlp file

在 jnlp 文件中

回答by ria

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.

如果您尝试在较旧版本的 jre 上运行 Java 文件而不是编译它的版本,则会发生 UnsupportedClassVersionError。

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

java.lang.UnsupportedClassVersionError: 在运行编译的 java 类文件时,.class 文件中的错误版本号 [at java.lang.ClassLoader.defineClass1(Native Method)]。

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

本质上,您不能运行使用比 JVM 更新的版本编译的 .class 文件。