[英]how to check jre version using java application
我已经在JDK7中创建了一个应用程序,但jre6仍然在市场上使用,如果我将我的jar文件发送给jre6的人,它就不会工作,有没有办法应用程序检查jre版本,如果它没有compat然后要求用户更新..
You can use System.getProperty(String key); 您可以使用System.getProperty(String key); method with "java.version"
as key. 以"java.version"
为键的方法。
String version = System.getProperty("java.version");
Example output: 示例输出:
1.6.0_30
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
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. 使用System.getProperty("java.version")
或System.getProperty("java.runtime.version")
来获取已安装的java版本。
The above code snipped helps you find out more details such as java vendor name , OS etc. 以上代码剪切可帮助您找到更多详细信息,如java供应商名称,操作系统等。
这是一个使用System.getProperty("java.version");
检查版本并在版本不正确时拖出异常的示例 System.getProperty("java.version");
Using Java Web Start you have a messagebox about the java version if it is not compatible with your jar. 使用Java Web Start,如果它与您的jar不兼容,您将拥有一个关于Java版本的消息框。 For example: 例如:
<resources>
<j2se version="1.4+"
href="http://java.sun.com/products/autodl/j2se" />
in the jnlp file 在jnlp文件中
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文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.