Linux JAVA_HOME 和 PATH 已设置,但 java -version 仍显示旧的

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

JAVA_HOME and PATH are set but java -version still shows the old one

javalinuxenvironment-variableslinux-mint

提问by

I am using Linux Mint Cinnamon 14. I have set the $JAVA_HOMEand $PATHenvironment variables in ~/.profileas follows:

我使用Linux Mint的肉桂14.我已设置$JAVA_HOME$PATH环境变量~/.profile如下:

export JAVA_HOME=/home/aqeel/development/jdk/jdk1.6.0_35
export PATH=/home/aqeel/development/jdk/jdk1.6.0_35/bin:$PATH

I then did source ~/.profileto make the proper changes.

然后我source ~/.profile做了适当的改变。

When I execute java -versioncommand to check the active java version, it shows the default (already installed open-jdk) java version. How can I override the default open-jdk with the one I downloaded?

当我执行java -version命令来检查活动的 java 版本时,它显示默认的(已经安装的 open-jdk)java 版本。如何用我下载的那个覆盖默认的 open-jdk?

UPDATE:

更新:

which javasays /usr/bin/java

which java说 /usr/bin/java

$JAVA_HOME/bin/java -versionsays 'Permission Denied'

$JAVA_HOME/bin/java -version说“权限被拒绝”

sudo $JAVA_HOME/bin/java -version(asks for password, then) says Command not found

sudo $JAVA_HOME/bin/java -version(然后要求输入密码)说找不到命令

but cd $JAVA_HOME/bin, and lsshows that it is right directory.

但是cd $JAVA_HOME/bin,并ls显示它是正确的目录。

采纳答案by Anders R. Bystrup

While it looks like your setup is correct, there are a few things to check:

虽然看起来您的设置是正确的,但仍有一些事项需要检查:

  1. The output of env- specifically PATH.
  2. command -v javatells you what?
  3. Is there a javaexecutable in $JAVA_HOME\binand does it have the execute bit set? If not chmod a+x javait.
  1. 的输出env- 具体来说PATH
  2. command -v java告诉你什么?
  3. 是否有java可执行文件$JAVA_HOME\bin,是否设置了执行位?如果不是chmod a+x java

I trust you have source'd your .profileafter adding/changing the JAVA_HOMEand PATH?

我相信source.profile在添加/更改JAVA_HOME和之后已经完成了PATH

Also, you can help yourself in future maintenance of your JDK installation by writing this instead:

此外,您可以通过编写以下代码来帮助自己将来维护 JDK 安装:

export JAVA_HOME=/home/aqeel/development/jdk/jdk1.6.0_35
export PATH=$JAVA_HOME/bin:$PATH

Then you only need to update one env variable when you setup the JDK installation.

那么你只需要在设置 JDK 安装时更新一个 env 变量。

Finally, you may need to run hash -rto clear the Bash program cache. Other shells may need a similar command.

最后,您可能需要运行hash -r以清除 Bash 程序缓存。其他 shell 可能需要类似的命令。

Cheers,

干杯,

回答by Peter Lawrey

$JAVA_HOME/bin/java -version says 'Permission Denied'

$JAVA_HOME/bin/java -version 显示“权限被拒绝”

If you cannot access or run code, it which be ignored if added to your path. You need to make it accessible and runnable or get a copy of your own.

如果您无法访问或运行代码,则将其添加到您的路径时将被忽略。您需要使其可访问和可运行,或者获取您自己的副本。

Do an

做一个

ls -ld $JAVA_HOME $JAVA_HOME/bin $JAVA_HOME/bin/java

to see why you cannot access or run this program,.

看看为什么你不能访问或运行这个程序,。

回答by Bineeth

There is an easy way, just remove the symbolic link from "/usr/bin". It will work.

有一个简单的方法,只需从“/usr/bin”中删除符号链接即可。它会起作用。

回答by SandeepGodara

When it searches for java it looks from left to right in path entries which are separated by : so you need to add the path of latest jdk/bin directory before /usr/bin, so when it searches it'll find the latest one and stop searching further.

当它搜索java时,它在路径条目中从左到右查找,这些条目以:分隔,因此您需要在/usr/bin之前添加最新的jdk/bin目录的路径,因此当它搜索时它会找到最新的并且停止进一步搜索。

i.e. PATH=/usr/java/jdk_1.8/bin:/usr/bin:..... and so on.

即 PATH=/usr/java/jdk_1.8/bin:/usr/bin:..... 等等。

then initialize user profile using command: source ~/.bash_profile

然后使用命令初始化用户配置文件: source ~/.bash_profile

and check with: [which java]

并检查:[which java]

you'll get the right one.

你会得到正确的。

回答by javaProgrammer

Updating the ~/.profileor ~/.bash_profiledoes not work sometimes. I just deleted JDK 6 and sourced .bash_profile.

更新~/.profile~/.bash_profile有时不起作用。我刚刚删除了 JDK 6 和sourced .bash_profile

Try running this:

尝试运行这个:

sudo rm -rd jdk1.6.0_* #it may not let you delete without sudo

Then, modify/add your JAVA_HOME and PATH variables.

然后,修改/添加您的 JAVA_HOME 和 PATH 变量。

source ~/.bash_profile #assuming you've updated $JAVA_HOME and $PATH

回答by JesseBoyd

In Linux Mint 18 Cinnamon be sure to check /etc/profile.d/jdk_home.shI renamed this file to jdk_home.sh.old and now my path does not keep getting overridden and I can call java -version and see Java 9 as expected. Even though I correctly selected Java 9 in update-aternatives --config javathis jdk_home.sh file kept overriding the $PATH on boot-up.

在 Linux Mint 18 Cinnamon 中一定要检查/etc/profile.d/jdk_home.sh我将此文件重命名为 jdk_home.sh.old 现在我的路径不会被覆盖,我可以调用 java -version 并将 Java 9 视为预期的。尽管我在update-aternatives --config java这个 jdk_home.sh 文件中正确选择了 Java 9,但在启动时一直覆盖 $PATH。

回答by Gerardo Roza

If you want to use JDKs downloaded from Oracle's site, what worked for me (using Mint) is using update-alternatives:

如果您想使用从 Oracle 网站下载的 JDK,对我有用的(使用 Mint)是使用update-alternatives

  1. I downloaded the JDK and extracted it just anywhere, for example in /home/aqeel/development/jdk/jdk1.6.0_35
  2. I ran:

    sudo update-alternatives --install /usr/bin/java java /home/aqeel/development/jdk/jdk1.6.0_35/bin/java 1
    

    Now you canexecute sudo update-alternatives --config javaand choose your java version.

  3. This doesn't set the JAVA_HOME variable, which I wanted configured, so I just added it to my ~/.bashrc, including an export JAVA_HOME="/home/aqeel/development/jdk/jdk1.6.0_35"statement
  1. 我下载了 JDK 并将其解压缩到任何位置,例如在/home/aqeel/development/jdk/jdk1.6.0_35
  2. 我跑了:

    sudo update-alternatives --install /usr/bin/java java /home/aqeel/development/jdk/jdk1.6.0_35/bin/java 1
    

    现在您可以执行sudo update-alternatives --config java选择您的 Java 版本

  3. 这不会设置我想要配置的JAVA_HOME 变量,所以我只是将它添加到我的 ~/.bashrc 中,包括一条export JAVA_HOME="/home/aqeel/development/jdk/jdk1.6.0_35"语句

Now, I had two JDKs downloaded(let's say the second has been extracted to /home/aqeel/development/jdk/jdk-10.0.1).

现在,我下载两个 JDK(假设第二个已被提取到/home/aqeel/development/jdk/jdk-10.0.1)。

How can we change the JAVA_HOME dynamically based on the current java being used?

我们如何根据当前使用的 java 动态更改 JAVA_HOME?

My solution is not very elegant, I'm pretty sure there are better options out there, but anyway:

我的解决方案不是很优雅,我很确定那里有更好的选择,但无论如何:

  1. To change the JAVA_HOME dynamically based on the chosen java alternative, I added this snippet to the ~/.bashrc:

    export JAVA_HOME=$(update-alternatives --query java | grep Value: | awk -F'Value: ' '{print }' | awk -F'/bin/java' '{print }')
    
  1. 为了根据所选的 java 替代动态更改 JAVA_HOME,我将此代码段添加到 ~/.bashrc 中:

    export JAVA_HOME=$(update-alternatives --query java | grep Value: | awk -F'Value: ' '{print }' | awk -F'/bin/java' '{print }')
    

Finally (this is out of the scope) if you have to change the java version constantly, you might want to consider:

最后(这超出了范围)如果您必须不断更改 java 版本,您可能需要考虑:

  1. Adding an aliasto your ~./bash_aliases:

    alias change-java="sudo update-alternatives --config java"
    
  1. 为您的~./bash_aliases添加别名

    alias change-java="sudo update-alternatives --config java"
    

(You might have to create the file and maybe uncomment the section related to this in ~/.bashrc)

(您可能必须创建该文件,并可能在~/.bashrc 中取消注释与此相关的部分)

回答by shabinjo

check available Java versions on your Linux system by using update-alternatives command:

使用 update-alternatives 命令检查 Linux 系统上可用的 Java 版本:

 $ sudo update-alternatives --display java

Now that there are suitable candidates to change to, you can switch the default Java version among available Java JREs by running the following command:

现在有合适的候选版本可以更改,您可以通过运行以下命令在可用 Java JRE 之间切换默认 Java 版本:

 $ sudo update-alternatives --config java

When prompted, select the Java version you would like to use.1 or 2 or 3 or etc..

出现提示时,选择您要使用的 Java 版本。1 或 2 或 3 等。

Now you can verify the default Java version changed as follows.

现在您可以验证更改的默认 Java 版本如下。

 $ java -version

回答by rBay

Try this:

尝试这个:

  • export JAVA_HOME=put_here_your_java_home_path
  • type export PATH=$JAVA_HOME/bin:$PATH (ensure that $JAVA_HOME is the first element in PATH)
  • try java -version
  • 导出 JAVA_HOME=put_here_your_java_home_path
  • 输入 export PATH=$JAVA_HOME/bin:$PATH (确保 $JAVA_HOME 是 PATH 中的第一个元素)
  • 试试 java -version

Reason: there could be other PATH elements point to alternative java home. If you put first your preferred JAVA_HOME, the system will use this one.

原因:可能有其他 PATH 元素指向替代的 java home。如果你把你喜欢的JAVA_HOME放在第一位,系统就会使用这个。