如何在RHEL上使用Shell以编程方式获取已安装的JDK路径,当安装了双JDK时?

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

How to get the jdk install path using shell programmatically when dual jdk are installed in RHEL?

问题

我有一个服务器列表,我需要以编程方式找到jdk-8的安装位置。其中一些服务器只安装了一个jdk,可以使用shell命令readlink -f $(which java)/usr/java找到安装位置,这样更容易。但是某些RHEL服务器安装了多个jdk版本,包括11和8,并且alternatives --config java将设置为jdk 11,在这些情况下,使用上述命令难以获取jdk 8的安装位置。寻找一种shell脚本,在RHEL服务器上安装了多个jdk版本时,无论是单个还是多个jdk安装,都能找到jdk安装位置的方法。

英文:

I have a list of servers in which I need to find the jdk-8 install location programmatically. A few of the servers are having single jdk installation which makes it easier to find the install location using the shell command readlink -f $(which java) from /usr/java. But certain RHEL servers have multiple jdk installed both 11 and 8 and the alternative --config java would be set as jdk 11 , in these cases it is difficult to get the install location of the jdk 8 using the above command. Seeking shell script to find the jdk install location when there are multiple jdk version installation in RHEL server and works for both single and multiple jdk installation ?

答案1

得分: 1

在RHEL服务器上查找多个JDK版本安装位置的shell脚本,适用于单个和多个JDK安装?

我们不提供编写代码的服务,但我们将提供信息和建议,帮助您自行解决问题。

在使用alternatives系统来选择多个Java安装的机器上,您可以通过命令alternatives --display java获取有关所有配置的Java替代方案的信息。您还可以通过grep来筛选只选择主链接。示例:

[jbolling@random ~]$ alternatives --display java | grep '^/'
/usr/lib/jvm/java-11-openjdk-11.0.18.0.9-0.3.ea.el8.x86_64/bin/java - family java-11-openjdk.x86_64 priority 1
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.362.b08-3.el8.x86_64/jre/bin/java - family java-1.8.0-openjdk.x86_64 priority 1800362

然而,这已经假设Java 8 JDK可以通过alternatives系统获得,并且在alternatives中注册的路径中没有换行符。这是RHEL软件包的做法,但如果Java安装方式与官方软件包不同,那么可能不满足这些假设。此外,安装包安装后,也可以手动注销alternatives条目。

要进一步,您需要做其他假设,特别是您可以通过其路径识别您正在查找的Java安装。如果所有可用的Java安装都是通过官方RHEL软件包安装的,那么您只需在路径中查找"1.8.0"。因此,我们可以修改上一个命令如下:

[jbolling@random ~]$ alternatives --display java | grep '^/.*1[.]8[.]0'
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.362.b08-3.el8.x86_64/jre/bin/java - family java-1.8.0-openjdk.x86_64 priority 1800362

如果您希望grep仅解析java命令的路径,可以再进一步:

[jbolling@random ~]$ alternatives --display java | grep -o '^/.*1[.]8[.]0.*bin/java>'
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.362.b08-3.el8.x86_64/jre/bin/java

-o选项告诉grep只打印与正则表达式匹配的部分。将bin/java添加到模式中(可能)可以防止匹配扩展到注释字段,而\>只是额外保险,确保匹配在单词边界结束。

但这也意味着进一步假设java二进制文件位于其名称以bin结尾的子目录中。这是一个相当安全的假设,尤其是如果除官方软件包外没有其他Java安装,但它并不是绝对安全的。

英文:

> Seeking shell script to find the jdk install location when there are multiple jdk version installation in RHEL server and works for both single and multiple jdk installation ?

We are not a code-writing service, but we'll give you information and advice that may help you to serve yourself.

On a machine where the alternatives system is used to select between multiple Java installations, you can get information about all configured Java alternatives via the command alternatives --display java. You can furthermore filter that through grep to select just the master links. Example:

[jbolling@random ~]$ alternatives --display java | grep '^/'
/usr/lib/jvm/java-11-openjdk-11.0.18.0.9-0.3.ea.el8.x86_64/bin/java - family java-11-openjdk.x86_64 priority 1
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.362.b08-3.el8.x86_64/jre/bin/java - family java-1.8.0-openjdk.x86_64 priority 1800362

That already assumes, however, that the Java 8 JDK is available via the alternatives system, and that there are no newlines in the paths registered with alternatives. That is how the RHEL packages do things, but if Java is installed other than via the official packages, then it might not fulfill those assumptions. Also, it is possible to manually de-register an alternatives entry after the package is installed.

To go beyond that, you need to make additional assumptions -- specifically, that you can recognize the Java installation you are looking for from its path. If all the available Java installations were installed via official RHEL packages then you can just look for "1.8.0" in the path. Thus, we might revise the previous command to:

[jbolling@random ~]$ alternatives --display java | grep '^/.*1[.]8[.]0'
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.362.b08-3.el8.x86_64/jre/bin/java - family java-1.8.0-openjdk.x86_64 priority 1800362

And if you want grep to parse out just the path to the java command then you could go a bit further:

[jbolling@random ~]$ alternatives --display java | grep -o '^/.*1[.]8[.]0.*bin/java\>'
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.362.b08-3.el8.x86_64/jre/bin/java

The -o option tells grep to print only the part of the line that it matched to the regex. Adding bin/java to the pattern (probably) prevents the match from extending into the comment field, and the \> is just extra insurance that the match terminates at a word boundary.

That does, however, make the additional assumptions that the java binary is in a subdirectory whose name ends with bin. That is a fairly safe assumption, especially if there are no Java installations other than from official packages, but it is not absolutely safe.

huangapple
  • 本文由 发表于 2023年6月12日 20:07:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76456526.html
匿名

发表评论

匿名网友

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

确定