强制 Java 最低版本与“java -version:<value>”一起运行在 Windows 上不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6770293/
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
Enforcing Java minimum version to run with "java -version:<value>" doesn't work on Windows
提问by His
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.
我想强制执行我的应用程序应该运行的 JVM 的最低版本为 1.6 或更高(即 1.6+)。我的理解是,您可以使用“-version:”命令行参数来执行此操作。我试过了,它在 Linux 下似乎可以正常工作,但在 Windows 下却无法正常工作。
LINUX
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.
我在 Linux 机器上安装了 JDK 版本 1.6.0_21。$JAVA_HOME 和 $PATH 环境变量已设置为应有的值。
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.
一切似乎都在意料之中。“版本:1.6+”和“版本:1.5+”应该可以工作,因为我安装了 JDK 1.6.0_21,而“版本:1.7+”应该可以,因为我没有安装 JDK 1.7。
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.
我在 Windows 机器上安装了相同的 JDK 版本 1.6.0_21(更具体地说是 Windows 7)。%JAVA_HOME% 和 %PATH% 环境变量已设置为应有的值。
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.
谁能解释为什么相同的命令行参数在 Linux 上有效,而在 Windows 上无效?这是一个功能还是一个错误?
我能做些什么来修复/解决它?我希望尽可能在 Linux 和 Windows 上应用相同的命令行参数,因此我不必为 Linux 指定不同的“-version:”参数,而为 Windows 指定另一个不同的参数。
Thanks.
谢谢。
回答by Steffen Opel
- Can anyone explain why does the same command line argument work on Linux, but not on Windows? Is this a feature or a bug?
- 谁能解释为什么相同的命令行参数在 Linux 上有效,而在 Windows 上无效?这是一个功能还是一个错误?
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):
我不认为它与 Linux 与 Windows 相关,而是与特定发行版有关的问题 - 我现在可以使用 Oracle JDK 在 Windows 上很好地运行它,即 Sun JDK 的继任者(通过 PowerShell 或 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:
相反,它在带有 OpenJDK 的 Ubuntu 12.04 LTS 上对我来说失败了:
$ 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
- java -version:release 选项无法按照文档工作 (JDK-8011079)
- 引用的链接已损坏,当前链接为https://blogs.oracle.com/ksrini/entry/java_launcher_tricks_with_multiple;无论如何,我目前看不到引用的文档如何解决这个问题。
- java -version:1.6+ 拒绝工作 (JDK-6558219)
- 这似乎暗示了发行版的细节,确实有Java -version:spec对 Ubuntu 12.04 LTS不起作用 (#1050911)
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.
我已经确认这在 Ubuntu 13.10 和 Amazon Linux 2013.09.2 上的 OpenJDK 6/7 仍然会被破坏,所以我建议尝试不同的 OpenJDK 发行版或 JDK 供应商,以防这个问题很普遍——这绝对是一个奇怪的问题,尤其是 鉴于 OpenJDK 同时是官方的 Java SE 7 参考实现。
回答by user802421
How about doing in Java?
用Java怎么样?
String java = System.getProperty("java.specification.version");
double version = Double.valueOf(java);
if (version < 1.6) {
// Exit
}