简体   繁体   English

"Linux 中的 JAVA_HOME 目录"

[英]JAVA_HOME directory in Linux

Is there any linux command I could use to find out JAVA_HOME<\/code> directory?有没有我可以用来找出JAVA_HOME<\/code>目录的 linux 命令? I've tried print out the environment variables ("env") but I can't find the directory.我尝试打印出环境变量(“env”),但找不到目录。

"

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 javac would get you pretty close 我不确定窗户,但我想想where javac会让你非常接近

echo $JAVA_HOME will print the value if it's set. echo $JAVA_HOME将打印该值,如果已设置。 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. 如果您尝试使用which java并且找不到任何内容,则可能无法在您的计算机上安装Java,或者至少不在您的路径中。 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. 根据您拥有的Linux发行版以及是否具有root访问权限,您可以访问http://www.java.com下载所需的版本。 Then, you can set JAVA_HOME to point to this directory. 然后,您可以将JAVA_HOME设置为指向此目录。 Remember, that this is just a convention and shouldn't be used to determine if java is installed or not. 请记住,这只是一个约定,不应该用于确定是否安装了Java。

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

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"

To show the value of an environment variable you use: 要显示您使用的环境变量的值:

echo $VARIABLE echo $ VARIABLE

so in your case will be: 所以在你的情况下将是:

echo $JAVA_HOME echo $ JAVA_HOME

In case you don't have it setted, you can add in your .bashrc file: 如果您没有设置它,您可以添加.bashrc文件:

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

and it will dynamically change when you update your packages. 它会在您更新包时动态更改。

If $JAVA_HOME is 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 / jre used 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)

On the Terminal, type: 在终端上,键入:

echo "$JAVA_HOME" echo“$ JAVA_HOME”

If you are not getting anything, then your environment variable JAVA_HOME has not been set. 如果您没有得到任何东西,那么您的环境变量JAVA_HOME尚未设置。 You can try using "locate java" to try and discover where your installation of Java is located. 您可以尝试使用“locate java”来尝试发现Java的安装位置。

Did you set your JAVA_HOME 你有没有设置你的JAVA_HOME

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

Here's an improvement, grabbing just the directory to stdout: 这是一个改进,只抓住stdout的目录:

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

You can check from the command line by executing this command echo $JAVA_HOME<\/code> .您可以通过执行此命令echo $JAVA_HOME<\/code>从命令行进行检查。 If Java is installed but the path is not set, you need to identify the path to your java installation.如果安装了 Java 但未设置路径,则需要确定 Java 安装的路径。 I prefer using sudo update-alternatives --config java<\/code> which lists all installed versions with current active one marked and provides dialog to switch:我更喜欢使用sudo update-alternatives --config java<\/code>列出所有已安装版本并标记当前活动版本并提供对话框进行切换:

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:

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. 如果你真的想获得一些关于你的BASH的信息,请将该脚本放在你的.bashrc中并观看它。 You can scroll around and look it over. 你可以滚动并查看它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM