Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enforcing Java minimum version to run with "java -version:<value>" doesn't work on Windows

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.

like image 701
His Avatar asked Jul 21 '11 01:07

His


People also ask

How do you make Java and javac versions the same?

Go to Environment Variables in your windows machine. In User Variables : Make sure to set - Your user variable "JAVA_HOME" value to "C:\Program Files\Java\jdk-xxxx\bin" where "jdk-xxx" is the version of your jdk. In System Variable : - Add same "C:\Program Files\Java\jdk-xxxx\bin" value to "Path" variable.

Does Windows 10 have Java Runtime Environment?

Downloading the JRE InstallerThe JRE Installer is located on the Java SE Runtime Environment 10 Downloads page. In a browser, go to the Java SE Runtime Environment 10 Downloads page. The following JRE installers are available for you to download: Windows Offline: jre-10.

Can JDK and JRE be different versions?

The JDK and JRE versions can be different on the same computer. Multiple JDK and JRE versions are allowed on the same computer; it is better to find out which version is configured in the system classpath to run or compile the Java program.


1 Answers

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

like image 163
Steffen Opel Avatar answered Sep 28 '22 13:09

Steffen Opel