Linux JAVA_HOME 设置不正确。如何重置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9643964/
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
JAVA_HOME incorrectly set. How to reset it?
提问by user113454
When I try to run mvn
(Apache Maven, that is), I keep getting error "JAVA_HOME" not set.
当我尝试运行mvn
(即 Apache Maven)时,我不断收到error "JAVA_HOME" not set.
I follow the instructions to set the JAVA_HOME
variable as follow; In the terminal:
我按照说明设置JAVA_HOME
变量如下;在终端:
user@localhost$export JAVA_HOME=/home/user/jdk1.7.0_02/bin/java
user@localhost$export PATH=$PATH:/home/usr/jdk1.7.0_02/bin
That looks correct, right? Then how come I still getting the incorrect JAVA_HOME
error?
看起来是对的,对吧?那为什么我仍然收到不正确的JAVA_HOME
错误?
采纳答案by gavi
JAVA_HOME typically should only include the folder that contains the bin folder.
JAVA_HOME 通常应该只包含包含 bin 文件夹的文件夹。
So in your case
所以在你的情况下
export JAVA_HOME=/home/user/jdk1.7.0_02/
export JAVA_HOME=/home/user/jdk1.7.0_02/
export PATH=$PATH:$JAVA_HOME/bin
export PATH=$PATH:$JAVA_HOME/bin
In addition for finding the location of your java_home you can follow this command
除了查找 java_home 的位置之外,您还可以按照此命令
which java
(This will return the path of the current java binary. Over here its /usr/bin/java)
(这将返回当前 java 二进制文件的路径。这里是它的 /usr/bin/java)
ls -alh /usr/bin/java
ls -alh /usr/bin/java
( This will return true path to the symbolic link. Over here its /etc/alternatives/java.
(这将返回符号链接的真实路径。这里是它的 /etc/alternatives/java。
ls -alh /etc/alternatives/java
ls -alh /etc/alternatives/java
( This will return true path to this symbolic link which is actual JAVA HOME path)
(这将返回此符号链接的真实路径,即实际的 JAVA HOME 路径)
回答by Eduardo Costa
No... $JAVA_HOME must point to /home/user/jdk1.7.0_02/
不... $JAVA_HOME 必须指向 /home/user/jdk1.7.0_02/
To prevent errors like "/home/user" versus "/home/usr", $PATH should be "$PATH:$JAVA_HOME/bin"
为了防止“/home/user”与“/home/usr”之类的错误,$PATH 应该是“$PATH:$JAVA_HOME/bin”
And I recommend using a native package (yum, apt-get, etc).
我建议使用本机包(yum、apt-get 等)。
回答by Eduardo Costa
Goto Terminal and open either of the following files using an editor of your choice (vim, nano, etc):
# nano /etc/profile
(or)
# nano /root/.bash_profile
(Instead of root you can also change your normal username.)
Now run the following commands:
# export JAVA_HOME="/opt/jdk1.6.0" # export PATH="/opt/jdk1.6.0/bin:$PATH"
Logout and logon the system , now check the java version in your terminal using the following command:
# java -version
The output should look similar to this:
# java -version java version “1.6.0″ Java(TM) SE Runtime Environment (build 1.6.0-b105) Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)
转到终端并使用您选择的编辑器(vim、nano 等)打开以下任一文件:
# nano /etc/profile
(或者)
# nano /root/.bash_profile
(您也可以更改普通用户名,而不是 root。)
现在运行以下命令:
# export JAVA_HOME="/opt/jdk1.6.0" # export PATH="/opt/jdk1.6.0/bin:$PATH"
注销并登录系统,现在使用以下命令检查终端中的 java 版本:
# java -version
输出应该类似于:
# java -version java version “1.6.0″ Java(TM) SE Runtime Environment (build 1.6.0-b105) Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)
回答by Brian Roach
Because that's not what you set JAVA_HOME to.
因为那不是您将 JAVA_HOME 设置为的。
http://maven.apache.org/download.html
http://maven.apache.org/download.html
Make sure that JAVA_HOME is set to the location of your JDK, e.g. export JAVA_HOME=/usr/java/jdk1.5.0_02 and that $JAVA_HOME/bin is in your PATH environment variable.
确保 JAVA_HOME 设置为您的 JDK 的位置,例如 export JAVA_HOME=/usr/java/jdk1.5.0_02 并且 $JAVA_HOME/bin 在您的 PATH 环境变量中。
回答by Torben
You could put the following in your .bashrc, then it should be correct even if you change to a different java.
您可以将以下内容放在您的 .bashrc 中,那么即使您更改为不同的 java,它也应该是正确的。
a=`realpath /usr/bin/java`;
export JAVA_HOME="${a%/bin/java}"