CentOS中的“which java”打印错误的java路径

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

"which java" in CentOS prints wrong java path

javalinuxcentosjava-home

提问by sunskin

I am not sure why "which java" and "whereis java" paths are not correct. I tried to edit ~/.bash_profile and /etc/environment but did not help. The desired path is what is seen in "echo $JAVA_HOME" below but the same is not reflected in "which java"

我不确定为什么“ which java”和“ whereis java”路径不正确。我试图编辑 ~/.bash_profile 和 /etc/environment 但没有帮助。所需的路径是在下面的“ echo $JAVA_HOME”中看到的,但在“ which java”中没有反映出来

Below is what I get in CentOS 6.4:

以下是我在 CentOS 6.4 中得到的:

which java

哪个java

/usr/bin/java

/usr/bin/java



java -version

版本

java version "1.7.0_45"

java版本“1.7.0_45”

JAVA(TM) SE Runtime Environment (build 1.7.0_45-b18)

JAVA(TM) SE 运行时环境(构建 1.7.0_45-b18)

JAVA HotSpot (TM) 64-bit Server VM (build 24.45-b08, mixed mode)

JAVA HotSpot (TM) 64 位服务器 VM(构建 24.45-b08,混合模式)



whereis java

java在哪里

java: /usr/bin/java /etc/java /usr/lib/java /usr/share/java

java: /usr/bin/java /etc/java /usr/lib/java /usr/share/java



echo $JAVA_HOME

回声 $JAVA_HOME

/usr/java/jdk1.7.0_45/jre => desired shows correct when echo $JAVA_HOME

/usr/java/jdk1.7.0_45/jre => 当回显 $JAVA_HOME 时所需显示正确



采纳答案by Henry Finucane

Run alternatives --config javato pick the Java version you want to use as default. It will print out a list of installed Javas to choose from.

运行alternatives --config java以选择要用作默认值的 Java 版本。它将打印出已安装的 Java 列表以供选择。

which java, however, will always print out /usr/bin/java. This doesn't mean it's set wrong! Observe:

which java,但是,总是会打印出来/usr/bin/java。这并不意味着它设置错误!观察:

$ ls -l `which java`
lrwxrwxrwx 1 root root 22 Oct 19 11:49 /usr/bin/java -> /etc/alternatives/java
$ ls -l /etc/alternatives/java
lrwxrwxrwx 1 root root 35 Oct 19 11:49 /etc/alternatives/java -> /usr/lib/jvm/jre-1.5.0-gcj/bin/java

If you use alternativesto change the path to IcedTea, ls -l /etc/alternatives/javawill reflect that.

如果你用alternatives改变路径到IcedTea,ls -l /etc/alternatives/java就会反映出来。

回答by Peter Lawrey

Your PATH (and nothing else) determines which directories to look for commands. This is the same in Linux, Solaris, and DOS.

您的 PATH(以及其他任何东西)决定了要查找命令的目录。这在 Linux、Solaris 和 DOS 中是一样的。

When you do a which {command}it find the first directory you can execute the command in.

当您执行 a 时,which {command}它会找到您可以在其中执行命令的第一个目录。

When you update your PATH in .bashrc, you have to source it again to change your current settings.

当您在 中更新 PATH 时.bashrc,您必须再次获取它以更改当前设置。

回答by nitinr708

Sometimes alternatives does not work in a single command by selecting the desired version of java. I am not sure of the precise reason for this though..

有时,通过选择所需的 java 版本,替代方法在单个命令中不起作用。虽然我不确定这的确切原因..

I fell victim to such a scenario. The auto-pilot failed, we must fly manual now..

我成为这种情况的受害者。自动驾驶失败,我们现在必须手动飞行..

In any of below two files in your unix installation add following variables and a call to a shell script (I have provided below) -

在您的 unix 安装中的以下两个文件中的任何一个中添加以下变量和对 shell 脚本的调用(我在下面提供了)-

~/.bashrc

~/.bashrc

export JAVA_HOME=/opt/jdk1.8.0_141
export PATH=$JAVA_HOME/bin:$PATH
sudo bash /appl/common/toFixJava.sh

OR

或者

/etc/profile

/etc/配置文件

export JAVA_HOME=/opt/jdk1.8.0_141
export PATH=$JAVA_HOME/bin:$PATH
sudo bash /appl/common/toFixJava.sh

The script below referenced above sets the rest of java modules to utilize the java you want to use -

下面引用的脚本将其余的 java 模块设置为使用您要使用的 java -

toFixJava.sh

toFixJava.sh

altrs="java javac jre jarsigner javadoc javafxpackager javah javap java-rmi.cgi javaws jcmd jconsole jcontrol jdb jhat jinfo jmap jmc jmc.ini jps jrunscript jsadebugd jstack jstat jstatd jvisualvm keytool appletviewer apt ControlPanel extcheck idlj native2ascii orbd pack200 policytool rmic rmid rmiregistry schemagen serialver servertool tnameserv unpack200 wsgen wsimport xjc"

for each in $altrs do

对于 $altrs 中的每个做

alternatives --install /usr/bin/$each $each /opt/jdk1.8.0_141/bin/$each <desired installation index number, e.g. 2>
alternatives --set $each /opt/jdk1.8.0_141/bin/$each

done

完毕

Please do let know if this doesn't work for you. I will help you solve alternatively!

如果这对您不起作用,请告诉我们。我来帮你解决吧!