AlBlue
AlBlue

Reputation: 24060

How can I determine whether the installed Java supports modules or not?

If I have a launcher script, how do I determine whether the JVM supports modules or not (e.g. 9+) to decide whether to launch with -classpath or --module script?

Upvotes: 1

Views: 1442

Answers (1)

AlBlue
AlBlue

Reputation: 24060

Approaches

There are several ways the information can be found. It's possible to parse the exact Java version or look for differences that are only found in a modular JVM or in non-modular JVMs.

Running java -version

Obviously it's possible to run the java command with a -version and parse the output:

% java -version
openjdk version "11.0.10" 2021-01-19 LTS
OpenJDK Runtime Environment Zulu11.45+27-CA (build 11.0.10+9-LTS)
OpenJDK 64-Bit Server VM Zulu11.45+27-CA (build 11.0.10+9-LTS, mixed mode)

However, this involves launching a process, running a number of setup/initialisation options, and validating/verifying other flags. This means that while quick, it won't be nearly instantaneous:

% time java -version
0.08s user 0.02s system 101% cpu 0.100 total

Running java -Xinternalversion

There is an internal option which isn't widely known about, but is documented in the -java -X flags, that doesn't instantiate the Java classes but simply returns the state of the binary:

-Xinternalversion
    displays more detailed JVM version information than the
    -version option

This launches slightly faster than the above:

% time java -Xinternalversion
OpenJDK 64-Bit Server VM (11.0.10+9-LTS) for bsd-amd64 JRE (Zulu11.45+27-CA) (11.0.10+9-LTS),
built on Dec 30 2020 12:39:54 by "zulu_re" with gcc 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)
0.01s user 0.01s system 88% cpu 0.029 total

This also works on OpenJ9 images:

% java -Xinternalversion
Eclipse OpenJ9 OpenJDK 64-bit Server VM (11.0.10+9) from linux-amd64 JRE
with Extensions for OpenJDK for Eclipse OpenJ9 11.0.10.0,
built on Jan 20 2021 08:58:22 by  with g++-7.5 (GCC) 7.5.0

Reading JAVA_HOME/release file

If launching a process is considered too expensive, then reading or detecting files can be used instead. OpenJDK derived JVMs have a release file, which contains information about the Java version:

% grep VERSION $JAVA_HOME/release
IMPLEMENTOR_VERSION="Zulu11.45+27-CA"
JAVA_VERSION="11.0.10"
JAVA_VERSION_DATE="2021-01-19"

% grep VERSION $JAVA_8_HOME/release
JAVA_VERSION="1.8.0_282"
OS_VERSION="11.2"

Reading JAVA_HOME/lib/modules file

There are additional files that exist in a modular JVM (as part of the JEP 220 refactoring) which can be used to detect if the JVM supports modules or not. A modular JVM will have a $JAVA_HOME/lib/modules file with magic value 0xcafedada, which is understood by the file command as a module file:

% file $JAVA_HOME/lib/modules 
/Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home/lib/modules: Java module image (little endian), version 1.0

The release file and the lib/modules exist in the OpenJDK and images produced by jlink. The release file exists on Java 8 and above, but the lib/modules only exists on Java 9 and above.

Some versions of Docker's OpenJDK image do not have the release file. Others install the Java version into a different location, which means that the JAVA_HOME (or equivalent) may not be found. If 100% guarantees are required that the same version of java is used in the path to launch the app as well as find the version, java -Xinternalversion is guaranteed to work and give the exact answer.

Looking for rt.jar/classes.zip

Another way of testing for the existence of a pre-modular JVM is to see whether rt.jar (or for really old JVMs, classes.zip) exists in JAVA_HOME/jre/lib. These files are missing in Java 9 and above.

Summary

If you already have JAVA_HOME, then the fastest way is to determine the existence of the JAVA_HOME/lib/modules file. For correctness, you could verify the magic number 0xcafedada (either little-endian or big-endian). However, this file doesn't exist on Java 8 or below but does on Java 9 and above, including jlink produced images.

Data

Collected data from various platforms:

openjdk docker images

(aka 'mystery meat' images) from https://hub.docker.com/_/openjdk/

for i in {7..15}
do
  echo
  echo openjdk:$i
  docker run --rm -it openjdk:$i sh -c '
    ls -l $(dirname $(readlink -f $(command -v java)))/../lib/modules
    grep JAVA_VERSION= $(dirname $(readlink -f $(command -v java)))/../release 
    java -Xinternalversion'
done

openjdk:7
ls: cannot access /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/../lib/modules: No such file or directory
grep: /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/../release: No such file or directory
OpenJDK 64-Bit Server VM (24.221-b02) for linux-amd64 JRE (1.7.0_221-b02), built on May  9 2019 19:23:05 by "pbuilder" with gcc 4.9.2

openjdk:8
ls: cannot access '/usr/local/openjdk-8/bin/../lib/modules': No such file or directory
JAVA_VERSION="1.8.0_282"
OpenJDK 64-Bit Server VM (25.282-b08) for linux-amd64 JRE (1.8.0_282-b08), built on Jan 11 2021 02:10:15 by "openjdk" with gcc 4.4.7 20120313 (Red Hat 4.4.7-23)

openjdk:9
-rw-r--r-- 1 root root 157113207 Apr  2  2018 /usr/lib/jvm/java-9-openjdk-amd64/bin/../lib/modules
grep: /usr/lib/jvm/java-9-openjdk-amd64/bin/../release: No such file or directory
OpenJDK 64-Bit Server VM (9.0.4+12-Debian-4) for linux-amd64 JRE (9.0.4+12-Debian-4), built on Apr  2 2018 07:28:15 by "buildd" with gcc 7.3.0

openjdk:10
-rw-r--r-- 1 root root 156084936 Oct 21  2018 /usr/lib/jvm/java-10-openjdk-amd64/bin/../lib/modules
grep: /usr/lib/jvm/java-10-openjdk-amd64/bin/../release: No such file or directory
OpenJDK 64-Bit Server VM (10.0.2+13-Debian-2) for linux-amd64 JRE (10.0.2+13-Debian-2), built on Oct 21 2018 10:11:46 by "buildd" with gcc 8.2.0

openjdk:11
-rw-rw-r-- 1 root root 142005674 Jan 12 10:31 /usr/local/openjdk-11/bin/../lib/modules
JAVA_VERSION="11.0.10"
OpenJDK 64-Bit Server VM (11.0.10+9) for linux-amd64 JRE (11.0.10+9), built on Jan 12 2021 05:23:33 by "openjdk" with gcc 4.4.7 20120313 (Red Hat 4.4.7-23)

openjdk:12
-rw-r--r-- 1 668 668 138934817 Jul 16  2019 /usr/java/openjdk-12/bin/../lib/modules
JAVA_VERSION="12.0.2"
OpenJDK 64-Bit Server VM (12.0.2+10) for linux-amd64 JRE (12.0.2+10), built on Jul 16 2019 00:57:08 by "mach5one" with gcc 7.3.0

openjdk:13
-rw-r--r-- 1 10668 10668 140052206 Dec 11  2019 /usr/java/openjdk-13/bin/../lib/modules
JAVA_VERSION="13.0.2"
OpenJDK 64-Bit Server VM (13.0.2+8) for linux-amd64 JRE (13.0.2+8), built on Dec 11 2019 09:23:26 by "mach5one" with gcc 8.2.0

openjdk:14
-rw-r--r-- 1 root root 143828463 Jul  8  2020 /usr/java/openjdk-14/bin/../lib/modules
JAVA_VERSION="14.0.2"
OpenJDK 64-Bit Server VM (14.0.2+12-46) for linux-amd64 JRE (14.0.2+12-46), built on Jul  8 2020 23:30:21 by "mach5one" with gcc 8.3.0

openjdk:15
-rw-r--r-- 1 root root 137265245 Dec  7 20:09 /usr/java/openjdk-15/bin/../lib/modules
JAVA_VERSION="15.0.2"
OpenJDK 64-Bit Server VM (15.0.2+7-27) for linux-amd64 JRE (15.0.2+7-27), built on Dec  7 2020 19:54:59 by "mach5one" with gcc 9.2.0

AdoptOpenJDK Hotspot images

From https://hub.docker.com/_/adoptopenjdk

for i in 8 {11..15}
do
  echo
  echo adoptopenjdk:$i-hotspot
  docker run --rm -it adoptopenjdk:$i-hotspot sh -c '
    ls -l $(dirname $(readlink -f $(command -v java)))/../lib/modules
    grep JAVA_VERSION= $(dirname $(readlink -f $(command -v java)))/../release
    java -Xinternalversion'
done

adoptopenjdk:8-hotspot
ls: cannot access '/opt/java/openjdk/bin/../lib/modules': No such file or directory
JAVA_VERSION="1.8.0_282"
OpenJDK 64-Bit Server VM (25.282-b08) for linux-amd64 JRE (1.8.0_282-b08), built on Jan 20 2021 11:56:52 by "jenkins" with gcc 7.5.0

adoptopenjdk:11-hotspot
-rw-r--r-- 1 root root 142006963 Jan 20 12:17 /opt/java/openjdk/bin/../lib/modules
JAVA_VERSION="11.0.10"
OpenJDK 64-Bit Server VM (11.0.10+9) for linux-amd64 JRE (11.0.10+9), built on Jan 20 2021 12:11:51 by "" with gcc 7.5.0

adoptopenjdk:12-hotspot
-rw-r--r-- 1 500 500 141885840 Jul 18  2019 /opt/java/openjdk/bin/../lib/modules
JAVA_VERSION="12.0.2"
OpenJDK 64-Bit Server VM (12.0.2+10) for linux-amd64 JRE (12.0.2+10), built on Jul 18 2019 14:41:47 by "jenkins" with gcc 7.3.1 20180303 (Red Hat 7.3.1-5)

adoptopenjdk:13-hotspot
-rw-r--r-- 1 root root 143013610 Jan 17  2020 /opt/java/openjdk/bin/../lib/modules
JAVA_VERSION="13.0.2"
OpenJDK 64-Bit Server VM (13.0.2+8) for linux-amd64 JRE (13.0.2+8), built on Jan 17 2020 15:30:29 by "jenkins" with gcc 7.3.1 20180303 (Red Hat 7.3.1-5)

adoptopenjdk:14-hotspot
-rw-r--r-- 1 root root 155307303 Jul 15  2020 /opt/java/openjdk/bin/../lib/modules
JAVA_VERSION="14.0.2"
OpenJDK 64-Bit Server VM (14.0.2+12) for linux-amd64 JRE (14.0.2+12), built on Jul 15 2020 09:06:52 by "" with gcc 7.5.0

adoptopenjdk:15-hotspot
-rw-r--r-- 1 root root 147653756 Jan 21 12:09 /opt/java/openjdk/bin/../lib/modules
JAVA_VERSION="15.0.2"
OpenJDK 64-Bit Server VM (15.0.2+7) for linux-amd64 JRE (15.0.2+7), built on Jan 21 2021 11:55:36 by "" with gcc 7.5.0

AdoptOpenJDK OpenJ9 images

From https://hub.docker.com/_/adoptopenjdk using the -openj9 flavours

for i in 8 {11..15}
do
  echo
  echo adoptopenjdk:$i-openj9 
  docker run --rm -it adoptopenjdk:$i-openj9 sh -c ' 
    ls -l $(dirname $(readlink -f $(command -v java)))/../lib/modules
    grep JAVA_VERSION= $(dirname $(readlink -f $(command -v java)))/../release
    java -Xinternalversion'
done

adoptopenjdk:8-openj9
ls: cannot access '/opt/java/openjdk/bin/../lib/modules': No such file or directory
JAVA_VERSION="1.8.0_282"
Eclipse OpenJ9 OpenJDK 64-bit Server VM (1.8.0_282-b08) from linux-amd64 JRE with Extensions for OpenJDK for Eclipse OpenJ9 8.0.282.0, built on Jan 20 2021 08:35:35 by  with g++-7.5 (GCC) 7.5.0

adoptopenjdk:11-openj9
-rw-r--r-- 1 root root 124147727 Jan 20 09:22 /opt/java/openjdk/bin/../lib/modules
JAVA_VERSION="11.0.10"
Eclipse OpenJ9 OpenJDK 64-bit Server VM (11.0.10+9) from linux-amd64 JRE with Extensions for OpenJDK for Eclipse OpenJ9 11.0.10.0, built on Jan 20 2021 08:58:22 by  with g++-7.5 (GCC) 7.5.0

adoptopenjdk:12-openj9
-rw-r--r-- 1 500 500 125220100 Jul 19  2019 /opt/java/openjdk/bin/../lib/modules
JAVA_VERSION="12.0.2"
Eclipse OpenJ9 OpenJDK 64-bit Server VM (12.0.2+10) from linux-amd64 JRE with Extensions for OpenJDK for Eclipse OpenJ9 12.0.2.0, built on Jul 19 2019 19:53:58 by jenkins with g++ (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5)

adoptopenjdk:13-openj9
-rw-r--r-- 1 root root 124722695 Jan 17  2020 /opt/java/openjdk/bin/../lib/modules
JAVA_VERSION="13.0.2"
Eclipse OpenJ9 OpenJDK 64-bit Server VM (13.0.2+8) from linux-amd64 JRE with Extensions for OpenJDK for Eclipse OpenJ9 13.0.2.0, built on Jan 17 2020 10:38:21 by jenkins with g++ (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5)

adoptopenjdk:14-openj9
-rw-r--r-- 1 root root 128237302 Jul 15  2020 /opt/java/openjdk/bin/../lib/modules
JAVA_VERSION="14.0.2"
Eclipse OpenJ9 OpenJDK 64-bit Server VM (14.0.2+12) from linux-amd64 JRE with Extensions for OpenJDK for Eclipse OpenJ9 14.0.2.0, built on Jul 15 2020 14:33:14 by  with g++-7.5 (GCC) 7.5.0

adoptopenjdk:15-openj9
-rw-r--r-- 1 root root 122382952 Jan 21 08:41 /opt/java/openjdk/bin/../lib/modules
JAVA_VERSION="15.0.2"
Eclipse OpenJ9 OpenJDK 64-bit Server VM (15.0.2+7) from linux-amd64 JRE with Extensions for OpenJDK for Eclipse OpenJ9 15.0.2.0, built on Jan 21 2021 08:12:28 by  with g++-7.5 (GCC) 7.5.0

Azul Zulu images

From https://hub.docker.com/r/azul/zulu-openjdk

for i in 7 8 {11..15}
do
  echo
  echo azul/zulu-openjdk:$i
  docker run --rm -it azul/zulu-openjdk:$i sh -c '       
    ls -l $(dirname $(readlink -f $(command -v java)))/../lib/modules
    grep JAVA_VERSION= $(dirname $(readlink -f $(command -v java)))/../release
    java -Xinternalversion'
done

azul/zulu-openjdk:7
ls: cannot access '/usr/lib/jvm/zulu7-ca-amd64/jre/bin/../lib/modules': No such file or directory
grep: /usr/lib/jvm/zulu7-ca-amd64/jre/bin/../release: No such file or directory
OpenJDK 64-Bit Server VM (24.292-b07) for linux-amd64 JRE (Zulu 7.44.0.11-CA-linux64) (1.7.0_292-b07), built on Dec 25 2020 02:30:15 by "zulu_re" with gcc 4.4.7 20120313 (Red Hat 4.4.7-3)

azul/zulu-openjdk:8
ls: cannot access '/usr/lib/jvm/zulu8-ca-amd64/jre/bin/../lib/modules': No such file or directory
grep: /usr/lib/jvm/zulu8-ca-amd64/jre/bin/../release: No such file or directory
OpenJDK 64-Bit Server VM (25.282-b08) for linux-amd64 JRE (Zulu 8.52.0.23-CA-linux64) (1.8.0_282-b08), built on Jan 12 2021 07:57:43 by "zulu_re" with gcc 4.4.7 20120313 (Red Hat 4.4.7-3)

azul/zulu-openjdk:11
-rw-r--r-- 1 root root 144713046 Jan 13 21:49 /usr/lib/jvm/zulu11-ca-amd64/bin/../lib/modules
JAVA_VERSION="11.0.10"
OpenJDK 64-Bit Server VM (11.0.10+9-LTS) for linux-amd64 JRE (Zulu11.45+27-CA) (11.0.10+9-LTS), built on Dec 30 2020 23:45:15 by "zulu_re" with gcc 4.9.2 20150212 (Red Hat 4.9.2-6)

azul/zulu-openjdk:12
-rw-r--r-- 1 root root 141878585 Jul 11  2019 /usr/lib/jvm/zulu-12-amd64/bin/../lib/modules
JAVA_VERSION="12.0.2"
OpenJDK 64-Bit Server VM (12.0.2+3) for linux-amd64 JRE (Zulu12.3+11-CA) (12.0.2+3), built on Jul 11 2019 18:01:29 by "zulu_re" with gcc 7.3.1 20180303 (Red Hat 7.3.1-5)

azul/zulu-openjdk:13
-rw-r--r-- 1 root root 143053558 Jan 13 21:37 /usr/lib/jvm/zulu13-ca-amd64/bin/../lib/modules
JAVA_VERSION="13.0.6"
OpenJDK 64-Bit Server VM (13.0.6+5-MTS) for linux-amd64 JRE (13.0.6+5-MTS) (Zulu13.37+21-CA), built on Dec 26 2020 00:17:26 by "zulu_re" with gcc 7.3.1 20180303 (Red Hat 7.3.1-5)

azul/zulu-openjdk:14
-rw-r--r-- 1 root root 146757927 Jul 10  2020 /usr/lib/jvm/zulu-14-amd64/bin/../lib/modules
JAVA_VERSION="14.0.2"
OpenJDK 64-Bit Server VM (14.0.2+12) for linux-amd64 JRE (14.0.2+12) (Zulu14.29+23-CA), built on Jul 10 2020 22:15:57 by "zulu_re" with gcc 7.3.1 20180303 (Red Hat 7.3.1-5)

azul/zulu-openjdk:15
-rw-r--r-- 1 root root 140186164 Jan 22 17:19 /usr/lib/jvm/zulu15-ca-amd64/bin/../lib/modules
JAVA_VERSION="15.0.2"
OpenJDK 64-Bit Server VM (15.0.2+7) for linux-amd64 JRE (15.0.2+7) (Zulu15.29+15-CA), built on Jan 21 2021 06:01:59 by "zulu_re" with gcc 7.3.1 20180303 (Red Hat 7.3.1-5)

Upvotes: 5

Related Questions