问题
I want to enforce the minimum version of JVM my application should run on to 1.6 or greater (i.e. 1.6+). My understanding is that you can do this using the "-version:" command line argument. I tried it, and it seemed to work fine under Linux but not under Windows.
LINUX
I have a JDK version 1.6.0_21 installed on a Linux machine. The $JAVA_HOME and $PATH environment variables have been set to what they should be.
I ran the following:
$ java -version:1.6+ -version
java version "1.6.0_21"
Java(TM) SE Runtime Environment (build 1.6.0_21-b06)
Java HotSpot(TM) 64-Bit Server VM (build 17.0-b16, mixed mode)
$ java -version:1.5+ -version
java version "1.6.0_21"
Java(TM) SE Runtime Environment (build 1.6.0_21-b06)
Java HotSpot(TM) 64-Bit Server VM (build 17.0-b16, mixed mode)
$ java -version:1.7+ -version
Unable to locate JRE meeting specification "1.7+"
All seemed to be expected. "version:1.6+" and "version:1.5+" should work because I have a JDK 1.6.0_21 installed, and "version:1.7+" shouldn't because I don't have a JDK 1.7 installed.
WINDOWS
I have the same JDK version 1.6.0_21 installed on a Windows machine (Windows 7 to be more specific). The %JAVA_HOME% and %PATH% environment variables have been set to what they should be.
I ran the following:
$ java -version:1.6+ -version
Unable to locate JRE meeting specification "1.6+"
$ java -version:1.5+ -version
Unable to locate JRE meeting specification "1.5+"
$ java -version:1.7+ -version
Unable to locate JRE meeting specification "1.7+"
I got an error for each execution.
Can anyone explain why does the same command line argument work on Linux, but not on Windows? Is this a feature or a bug?
What can I do to fix/work around it? As much as possible I want to have the same command line arguments applied on both Linux and Windows, so I don't have to specify a different "-version:" argument for Linux and another different one for Windows.
Thanks.
回答1:
- Can anyone explain why does the same command line argument work on Linux, but not on Windows? Is this a feature or a bug?
I don't think it is related to Linux vs. Windows, rather an issue with particular distributions - I can run this just fine on Windows with the Oracle JDK now, i.e. the successor of the Sun JDK (both via PowerShell or CMD):
PS> java -version:1.7+ -version
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)
On the contrary, it fails for me on Ubuntu 12.04 LTS with OpenJDK:
$ java -version
java version "1.7.0_51"
OpenJDK Runtime Environment (IcedTea 2.4.4) (7u51-2.4.4-0ubuntu0.12.04.2)
OpenJDK 64-Bit Server VM (build 24.45-b08, mixed mode)
$ java -version:1.7+ -version
Error: Unable to locate JRE meeting specification "1.7+"
A short search reveals the following somewhat inconclusive issues:
- java -version:release option does not work as documented (JDK-8011079)
- The referenced link is broken, the current one is https://blogs.oracle.com/ksrini/entry/java_launcher_tricks_with_multiple; regardless, I currently fail to see how the referenced docs address the issue though.
- java -version:1.6+ refuses to work (JDK-6558219)
- This seems to hint on distribution specifics, and indeed there is Java -version:spec does not work (#1050911) for Ubuntu 12.04 LTS
I've confirmed this to still be broken with OpenJDK 6/7 on Ubuntu 13.10 and also on Amazon Linux 2013.09.2, so I suggest to experiment with a different OpenJDK distribution or JDK vendor, in case this issue is prevalent - it's definitely an odd issue, esp. given OpenJDK is meanwhile the official Java SE 7 Reference Implementation.
回答2:
How about doing in Java?
String java = System.getProperty("java.specification.version");
double version = Double.valueOf(java);
if (version < 1.6) {
// Exit
}
来源:https://stackoverflow.com/questions/6770293/enforcing-java-minimum-version-to-run-with-java-versionvalue-doesnt-work