RedHat – RPM如何自动选择正确的JVM版本?

huangapple go评论144阅读模式
英文:

RedHat - how RPM may automatically select correct JVM version?

问题

我正在分发我自己的RPM软件包,其中包含一个jar文件。我以RHEL 8为目标。

默认情况下,RHEL 8安装了Java 8。我的jar需要Java 11。

为了在缺失时能够自动地将其带入并安装,在我的RPM的"spec"中,我添加了对Java 11的依赖,就像这样:

Requires: java-11-openjdk-headless

……确实,Java 11软件包会与我的软件包一起被下载和安装。

为了执行我的jar文件,我运行以下命令:

java -jar <my.jar>

然而,似乎总是选择了Java 8,并且我的应用程序无法正常运行。如果我使用"alternatives"命令并选择Java 11,一切都能正常工作。但我希将正确的Java版本打包到"自包含"的RPM软件包中,而不需要执行额外的手动步骤。我不希望用户需要选择正确的Java版本,我希望这种情况可以自动发生。

在执行我的jar文件时,是否有办法自动选择正确的Java版本?

英文:

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 &lt;my.jar&gt;

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?

答案1

得分: 2

> 在我的jar文件被执行时,是否有可能以某种方式自动选择正确的Java版本?

如果您的客户以以下方式运行您的应用程序:

 $ java -jar some.jar

那么您的应用程序无法自动选择正确的Java版本。您隐含地使用了当前搜索路径上的Java版本;即java指向的内容。这由用户掌握。

如果您希望您的应用程序选择正确的版本,您需要编写一个包装脚本,该脚本知道发行版安装不同Java版本的目录路径。您应该能够通过从(例如)/usr/bin/java到实际可执行文件的符号链接进行解决。

这有两种变化:

  • 您可以让安装脚本进行选择,并将其嵌入生成的包装脚本中。
  • 您可以让包装脚本自行进行选择,可能会借助命令行选项或环境变量的指导。

(请注意,/etc/alternatives 机制不能直接解决此问题。这允许用户选择某个版本的(例如)java命令。但这通过修改/usr/bin/java的符号链接链来实现。它影响使用/usr/bin/java的所有内容...而不仅仅是您的应用程序。)

(另外,请注意尝试使用RPM依赖关系来解决此问题是行不通的。依赖关系确保安装所需版本的Java...并不代表实际上会使用它。)

英文:

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

huangapple
  • 本文由 发表于 2020年8月18日 21:00:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63469206.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定