How to check JRE version prior to launch?

2019-01-08 23:22发布

What's the best way to determine if the version of the JRE installed on a machine is high enough for the application which the user wants to run? Is there a way of doing it using java-only stuff? I'd like the solution to work on Windows/Linux/MacOSX - if the JRE version is too low a message should be displayed. Currently I'm getting an exception if i try to run it on Java 1.5 (the app is built for Java 1.6). If there's no universal solution, what's the best way to do it on Windows?

13条回答
Luminary・发光体
2楼-- · 2019-01-09 00:00

You can require a Java version when running the Java command, e.g. java -version:1.6* com.me.MyClass. Not sure if this works on all releases of Java, but it works OK on 1.6 anyway.

查看更多
三岁会撩人
3楼-- · 2019-01-09 00:00

For the launcher - Check the version in there.

Inside the APP; as above use System.getProperties();

Properties sProp = java.lang.System.getProperties();
String sVersion = sProp.getProperty("java.version");
sVersion = sVersion.substring(0, 3);
Float f = Float.valueOf(sVersion);
if (f.floatValue() < (float) 1.4) {
    System.out.println("Java version too low ....");
    System.exit(1);
}
...
查看更多
Animai°情兽
4楼-- · 2019-01-09 00:01

Here is the code to get the JRE version installed in a system.

var list = deployJava.getJREs();
var result = "";
result = list[0];
for (var i=1; i<list.length; i++)
{
    result += ", " + list[i];
} 
document.write("jre version : "+result);
查看更多
相关推荐>>
5楼-- · 2019-01-09 00:03

I find that WinRun4J works quite well for me (but then again I may be biased since I wrote it:-)). This lets you specify a minimum and/or maximum version of java allowed. It will pop up a message box to the user if a suitable JRE version is not found (and the message is customisable).

查看更多
干净又极端
6楼-- · 2019-01-09 00:10

Hmm .. call me a boring guy, but what's wrong with using Launch4J or any other native launcher, for instance.

Use a native launcher to check the JVM version before actually running your code. Java only solutions (in my book) only make sense when you deal with developers; once you hit end-users, you'll realize that they dont care about Java or its technical details at all. If you would have written your application in GW-Basic they would not care less as long as your application works.

If Java 1.6 is not installed, lauchner4j will point the user to the download page for JDK 1.6. Thats probably more suitable for your problem than doing magic in Java.

查看更多
萌系小妹纸
7楼-- · 2019-01-09 00:11

You might also consider using Commons-Launcher, which allows you to setup various environment settings, or perform pre-checks before calling your application.

http://commons.apache.org/launcher

查看更多
登录 后发表回答