Linux 中的 JAVA_HOME 目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1117398/
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-03 17:31:02  来源:igfitidea点击:

JAVA_HOME directory in Linux

javalinuxdirectory

提问by Progress Programmer

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

是否有任何 linux 命令可以用来查找JAVA_HOME目录?我试过打印出环境变量(“env”),但我找不到目录。

采纳答案by AdamC

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

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

If you try which javaand 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.comto 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.

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

回答by Michael Aaron Safyan

On the Terminal, type:

在终端上,键入:

echo "$JAVA_HOME"

回声“$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.

如果您什么也没得到,那么您的环境变量 JAVA_HOME 尚未设置。您可以尝试使用“locate java”来尝试发现您安装的 Java 所在的位置。

回答by Nizar Grira

Did you set your JAVA_HOME

你设置你的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
  • Korn 和 bash shell:导出 JAVA_HOME=jdk-install-dir
  • Bourne shell:JAVA_HOME=jdk-install-dir;export JAVA_HOME
  • C shell:setenv JAVA_HOME jdk-install-dir

回答by cevaris

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

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

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

仅供参考,如果您使用的是 Mac,目前 JAVA_HOME 位于

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

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

回答by jsdevel

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

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

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

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

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

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

回答by Paulo Fidalgo

To show the value of an environment variable you use:

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

echo $VARIABLE

回声 $VARIABLE

so in your case will be:

所以在你的情况下将是:

echo $JAVA_HOME

回声 $JAVA_HOME

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

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

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

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

and it will dynamically change when you update your packages.

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

回答by Douglas G. Allen

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

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.

如果你真的想得到一些关于你的 BASH 的信息,把那个脚本放在你的 .bashrc 中,然后看着它飞过。您可以滚动并查看它。

回答by dnozay

If $JAVA_HOMEis defined in your environment...

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

$ 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/jreused in case there are multiple installations.

此方法可确保您找到正确的jdk/jre使用的,以防有多个安装。

Or using strace:

或使用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)

回答by Parth Mehrotra

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

只是另一个解决方案,这是一个跨平台(使用java),并将您指向 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.

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

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

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

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

回答by Parth Mehrotra

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:]]*//'