His
His

Reputation: 6043

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.

Thanks.

Upvotes: 10

Views: 14824

Answers (2)

Steffen Opel
Steffen Opel

Reputation: 64741

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

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.

Upvotes: 5

user802421
user802421

Reputation: 7505

How about doing in Java?

String java = System.getProperty("java.specification.version");
double version = Double.valueOf(java);
if (version < 1.6) {
    // Exit
}

Upvotes: 4

Related Questions