如何在 Linux shell 脚本中确定和使用实际的 java 路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9057946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:16:32  来源:igfitidea点击:

How to determine and use the actual java path in a Linux shell script

javalinuxshell

提问by MountainX

In a bash shell script on Ubuntu/Mint, I need to create some symlinks in the /jre/lib/ext/ directory under the java installation directory.

在 Ubuntu/Mint 上的 bash shell 脚本中,我需要在 java 安装目录下的 /jre/lib/ext/ 目录中创建一些符号链接。

For example, if openjdk6 is the default java, /usr/bin/java points to:

例如,如果 openjdk6 是默认的 java,则 /usr/bin/java 指向:

/usr/lib/jvm/java-6-openjdk/jre/bin/java

I can find this in my script with:

我可以在我的脚本中找到它:

MYJAVAPATH=readlink -f `which java`

The path I would need in my shell script would be based on part of that path, plus the path fragment above:

我在 shell 脚本中需要的路径将基于该路径的一部分,加上上面的路径片段:

/usr/lib/jvm/java-6-openjdk/jre/lib/ext/

Can anyone tell me to to derive the path immediately above in a bash shell script? Thanks.

谁能告诉我在 bash shell 脚本中直接导出路径?谢谢。

采纳答案by Adam Zalcman

You can simply append the relative part ../lib/ext/

您可以简单地附加相关部分 ../lib/ext/

MYJAVAPATH="$(readlink -f $(which java))"
LIB_EXT="$(dirname ${MYJAVAPATH})/../lib/ext"

and then use ${LIB_EXT}.

然后使用${LIB_EXT}.

回答by Michael Ridley

You could use dirnamesuch as

你可以使用dirname诸如

javalink=`which java`
javapath=`readlink $javalink`
javadir=`dirname $javapath`