I distributing my own RPM package, which contains of jar file. I targeting RHEL 8.
By default, on RHEL 8 installed Java 8. My jar requires Java 11.
In order to bring it and install “automatically” in case missing, inside my RPM “spec” I added dependency on Java 11 like this:
Requires: java-11-openjdk-headless
…and indeed Java 11 package downloaded and installed together with mine.
In order to execute my jar I running the following command:
java -jar <my.jar>
However, seems like Java 8 is the one which being selected and my application fails to run properly. If I using “alternatives” and selecting Java 11 – everything works fine. But I want to provide my customers a “self-contained” RPM package, without need to perform additional manual steps. I don’t want them to select the correct Java version, I want this to happen somehow by itself.
Is it possible somehow to automatically select correct Java version when my jar being executed?
Advertisement
Answer
Is it possible somehow to automatically select correct Java version when my jar being executed?
If your customers run your application like this:
$ java -jar some.jar
then it is NOT possible for your application to select the correct version of Java. You are implicitly using the version of Java that is on the current search path; i.e. what java
points at. That is in the hands of the user.
If you want your application to chose the correct version, you will need to write a wrapper script that knows the pathnames for the directories where different Java versions are installed by the distro. You should be able to work this out by following the symlinks from (say) /usr/bin/java
to the actual executable.
There are two variations on this:
- You could have the installer script make the choice and embed that in the generated wrapper script.
- You could have the wrapper script make the choice itself, possibly with the guidance of a command-line option or an environment variable.
(Note that the /etc/alternatives
mechanism doesn’t directly help with this problem. That allows the user to select a version of (say) the java
command. But that works by modifying the symlink chains for /usr/bin/java
. It affects everything that use /usr/bin/java
… not just your application.)
(Also, note that trying to do this with RPM dependencies won’t work. The dependencies ensure that the required version of Java is installed … not that it is actually used.)