Linux 中的 JAVA_HOME 目录

发布于 2024-07-26 23:43:40 字数 82 浏览 3 评论 0原文

我可以使用任何 Linux 命令来查找 JAVA_HOME 目录吗? 我尝试打印出环境变量(“env”),但找不到该目录。

Is there any linux command I could use to find out JAVA_HOME directory? I've tried print out the environment variables ("env") but I can't find the directory.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

堇色安年 2024-08-02 23:43:40

在 Linux 上,您可以运行 $(dirname $(dirname $(readlink -f $(which javac))))

在 Mac 上,您可以运行 $(dirname $(readlink $(which javac)) ))/java_home

我不确定 Windows,但我想 where javac 会让你非常接近

On Linux you can run $(dirname $(dirname $(readlink -f $(which javac))))

On Mac you can run $(dirname $(readlink $(which javac)))/java_home

I'm not sure about windows but I imagine where javac would get you pretty close

绮筵 2024-08-02 23:43:40

这是另一个解决方案,它是跨平台的(使用 java),并为您指出 jre 的位置。

java -XshowSettings:properties -version 2>&1 > /dev/null | grep 'java.home'

输出java的所有当前设置,并找到名为java.home的设置。

对于 Windows,您可以使用 findstr 而不是 grep。

java -XshowSettings:properties -version 2>&1 | findstr "java.home"

Just another solution, this one's cross platform (uses java), and points you to the location of the jre.

java -XshowSettings:properties -version 2>&1 > /dev/null | grep 'java.home'

Outputs all of java's current settings, and finds the one called java.home.

For windows, you can go with findstr instead of grep.

java -XshowSettings:properties -version 2>&1 | findstr "java.home"
萌酱 2024-08-02 23:43:40

echo $JAVA_HOME 将打印该值(如果已设置)。 但是,如果您没有在启动脚本中手动设置它,则它可能未设置。

如果您尝试 which java 但没有找到任何内容,则 Java 可能未安装在您的计算机上,或者至少不在您的路径中。 根据您使用的 Linux 发行版以及您是否具有 root 访问权限,您可以访问 http://www.java.com 下载您需要的版本。 然后,您可以将JAVA_HOME设置为指向该目录。 请记住,这只是一个约定,不应用于确定 java 是否已安装。

echo $JAVA_HOME will print the value if it's set. However, if you didn't set it manually in your startup scripts, it probably isn't set.

If you try which java and it doesn't find anything, Java may not be installed on your machine, or at least isn't in your path. Depending on which Linux distribution you have and whether or not you have root access, you can go to http://www.java.com to download the version you need. Then, you can set JAVA_HOME to point to this directory. Remember, that this is just a convention and shouldn't be used to determine if java is installed or not.

独行侠 2024-08-02 23:43:40

我知道这已经晚了,但是这个命令搜索 /usr/ 目录来为您查找 java

sudo find /usr/ -name *jdk

结果仅供

/usr/lib/jvm/java-6-openjdk
/usr/lib/jvm/java-1.6.0-openjdk

参考,如果您使用的是 Mac,当前 JAVA_HOME 位于

/System/Library/Frameworks/JavaVM.framework/首页

I know this is late, but this command searches the /usr/ directory to find java for you

sudo find /usr/ -name *jdk

Results to

/usr/lib/jvm/java-6-openjdk
/usr/lib/jvm/java-1.6.0-openjdk

FYI, if you are on a Mac, currently JAVA_HOME is located at

/System/Library/Frameworks/JavaVM.framework/Home

紙鸢 2024-08-02 23:43:40

要显示您使用的环境变量的值:

回显$VARIABLE

所以在你的情况下将是:

回显$JAVA_HOME

如果您没有设置,您可以在 .bashrc 文件中添加:

导出 JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")

,当您更新软件包时,它会动态更改。

To show the value of an environment variable you use:

echo $VARIABLE

so in your case will be:

echo $JAVA_HOME

In case you don't have it setted, you can add in your .bashrc file:

export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")

and it will dynamically change when you update your packages.

岛徒 2024-08-02 23:43:40

如果您的环境中定义了 $JAVA_HOME...

$ echo $JAVA_HOME
$ # I am not lucky...

您可以从加载的类中猜测它。

$ java -showversion -verbose 2>&1 | head -1
[Opened /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75.x86_64/jre/lib/rt.jar]

此方法可确保您在存在多个安装的情况下找到使用的正确 jdk/jre

或者使用strace

$ strace -e open java -showversion 2>&1 | grep -m1 /jre/
open("/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75.x86_64/jre/bin/../lib/amd64/jli/tls/x86_64/libpthread.so.0", O_RDONLY) = -1 ENOENT (No such file or directory)

If $JAVA_HOME is defined in your environment...

$ echo $JAVA_HOME
$ # I am not lucky...

You can guess it from the classes that are loaded.

$ java -showversion -verbose 2>&1 | head -1
[Opened /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75.x86_64/jre/lib/rt.jar]

This method ensures you find the correct jdk/jre used in case there are multiple installations.

Or using strace:

$ strace -e open java -showversion 2>&1 | grep -m1 /jre/
open("/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75.x86_64/jre/bin/../lib/amd64/jli/tls/x86_64/libpthread.so.0", O_RDONLY) = -1 ENOENT (No such file or directory)
橘亓 2024-08-02 23:43:40

在终端上,键入:

echo "$JAVA_HOME"

如果您没有收到任何信息,则说明您的环境变量 JAVA_HOME 尚未设置。 您可以尝试使用“locate java”来尝试查找 Java 安装位置。

On the Terminal, type:

echo "$JAVA_HOME"

If you are not getting anything, then your environment variable JAVA_HOME has not been set. You can try using "locate java" to try and discover where your installation of Java is located.

话少情深 2024-08-02 23:43:40

您是否设置了 JAVA_HOME

  • Korn 和 bash shell:export JAVA_HOME=jdk-install-dir
  • Bourne shell:JAVA_HOME=jdk-install-dir;export JAVA_HOME
  • C shell:setenv JAVA_HOME jdk-install-dir

Did you set your JAVA_HOME

  • Korn and bash shells:export JAVA_HOME=jdk-install-dir
  • Bourne shell:JAVA_HOME=jdk-install-dir;export JAVA_HOME
  • C shell:setenv JAVA_HOME jdk-install-dir
擦肩而过的背影 2024-08-02 23:43:40

这是一个改进,仅将目录抓取到标准输出:

java -XshowSettings:properties -version 2>&1 \
   | sed '/^[[:space:]]*java\.home/!d;s/^[[:space:]]*java\.home[[:space:]]*=[[:space:]]*//'

Here's an improvement, grabbing just the directory to stdout:

java -XshowSettings:properties -version 2>&1 \
   | sed '/^[[:space:]]*java\.home/!d;s/^[[:space:]]*java\.home[[:space:]]*=[[:space:]]*//'
腹黑女流氓 2024-08-02 23:43:40

您可以通过执行此命令 echo $JAVA_HOME 从命令行进行检查。 如果安装了 Java 但未设置路径,则需要确定 Java 安装的路径。 我更喜欢使用 sudo update-alternatives --config java ,它列出了所有已安装的版本,并标记了当前活动的版本,并提供了切换对话框:

There are 3 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
   1           java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.14.0.9-2.fc35.x86_64/bin/java)
   2           java-17-openjdk.x86_64 (/usr/lib/jvm/java-17-openjdk-17.0.2.0.8-1.fc35.x86_64/bin/java)
*+ 3           /usr/java/jdk-17.0.2/bin/java

Enter to keep the current selection[+], or type selection number: 

从上面的列表中,您可以选择您想要的 java 版本默认值。 例如,要将 JAVA_HOME 设置为选项 3,您可以这样做 export JAVA_HOME=/usr/java/jdk-17.0.2

You can check from the command line by executing this command echo $JAVA_HOME. If Java is installed but the path is not set, you need to identify the path to your java installation. I prefer using sudo update-alternatives --config java which lists all installed versions with current active one marked and provides dialog to switch:

There are 3 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
   1           java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.14.0.9-2.fc35.x86_64/bin/java)
   2           java-17-openjdk.x86_64 (/usr/lib/jvm/java-17-openjdk-17.0.2.0.8-1.fc35.x86_64/bin/java)
*+ 3           /usr/java/jdk-17.0.2/bin/java

Enter to keep the current selection[+], or type selection number: 

from the above list, you can select the version of java you want to be the default. To set the JAVA_HOME to option 3 for instance you can do it this way export JAVA_HOME=/usr/java/jdk-17.0.2

夜血缘 2024-08-02 23:43:40
export JAVA_HOME=$(realpath $(which java) | sed 's/\/bin\/.*//')

说明:

  • realpath 获取解析符号链接的绝对路径
  • sed 's/\/bin\/.*//' 删除 /bin 中的所有内容等效地

:(

export JAVA_HOME=$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")

信用:https://stackoverflow.com/a/29622512/2314737

(假设Java 已安装)

export JAVA_HOME=$(realpath $(which java) | sed 's/\/bin\/.*//')

Explanation:

  • realpath gets the absolute path resolving symbolic links
  • sed 's/\/bin\/.*//' removes everything from /bin on from the path

Equivalently:

export JAVA_HOME=$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")

(credit: https://stackoverflow.com/a/29622512/2314737)

(assuming that Java is installed)

你是我的挚爱i 2024-08-02 23:43:40

http://www.gnu .org/software/sed/manual/html_node/Print-bash-environment.html#Print-bash-environment

如果您确实想获取有关 BASH 的一些信息,请将该脚本放入您的 .bashrc 中并观察它的运行经过。 您可以滚动并查看它。

http://www.gnu.org/software/sed/manual/html_node/Print-bash-environment.html#Print-bash-environment

If you really want to get some info about your BASH put that script in your .bashrc and watch it fly by. You can scroll around and look it over.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文