已经设置了JAVA_HOME和PATH,但是java -version仍然显示旧的那个

时间:2021-11-12 23:06:08

I am using Linux Mint Cinnamon 14. I have set the $JAVA_HOME and $PATH environment variables in ~/.profile as follows:

我用的是Linux薄荷肉桂14。我在~/中设置了$JAVA_HOME和$PATH环境变量。配置文件如下:

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 ~/.profile to make the proper changes.

然后我做了来源~/。配置文件进行适当的更改。

When I execute java -version command 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版本时,它显示了默认的(已经安装了开放jdk) java版本。如何用我下载的那个来覆盖默认的open-jdk ?

UPDATE:

更新:

which java says /usr/bin/java

java说/usr/bin/java

$JAVA_HOME/bin/java -version says '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 ls shows that it is right directory.

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

9 个解决方案

#1


53  

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

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

  1. The output of env - specifically PATH.
  2. env的输出-特别是路径。
  3. which java tells you what?
  4. 哪个java告诉你什么?
  5. Is there a java executable in $JAVA_HOME\bin and does it have the execute bit set? If not chmod a+x java it.
  6. $JAVA_HOME\bin中是否有java可执行文件,它是否设置了执行位?如果不是chmod a+x java。

I trust you have source'd your .profile after adding/changing the JAVA_HOME and PATH?

我相信您在添加/更改了JAVA_HOME和PATH之后已经获得了.profile文件?

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

此外,您还可以通过编写日志来帮助您维护JDK安装

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

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

代替。然后,在安装JDK安装时,只需要更新一个env变量。

Cheers,

欢呼,

#2


11  

update-java-alternatives

update-java-alternatives

The java executable is not found with your JAVA_HOME, it only depends on your PATH.

java可执行文件在JAVA_HOME中没有找到,它只取决于您的路径。

update-java-alternatives is a good way to manage it for the entire system is through:

更新-java-alternative是管理整个系统的一个好方法:

update-java-alternatives -l

Sample output:

样例输出:

java-7-oracle 1 /usr/lib/jvm/java-7-oracle
java-8-oracle 2 /usr/lib/jvm/java-8-oracle

Choose one of the alternatives:

选择一种选择:

sudo update-java-alternatives -s java-7-oracle

Like update-alternatives, it works through symlink management. The advantage is that is manages symlinks to all the Java utilities at once: javac, java, javap, etc.

与更新替代方案一样,它通过符号链接管理工作。优点是可以同时管理到所有Java实用程序的符号链接:javac、Java、javap等等。

I am yet to see a JAVA_HOME effect on the JDK. So far, I have only seen it used in third-party tools, e.g. Maven.

我还没有看到JDK上的JAVA_HOME效果。到目前为止,我只在第三方工具(如Maven)中使用过。

#3


4  

$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,.

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

#4


2  

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.

例如路径= / 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]

检查:[哪个java]

you'll get the right one.

你会找到正确的。

#5


0  

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

有一个简单的方法,就是从“/usr/bin”中删除符号链接。它将工作。

#6


0  

Updating the ~/.profile or ~/.bash_profile does not work sometimes. I just deleted JDK 6 and sourced .bash_profile.

更新~ /。配置文件或~ /。bash_profile有时不工作。我刚刚删除了JDK 6并引用了。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和路径变量。

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

#7


0  

In Linux Mint 18 Cinnamon be sure to check /etc/profile.d/jdk_home.sh I 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 java this jdk_home.sh file kept overriding the $PATH on boot-up.

在Linux Mint 18中,一定要检查/etc/profile.d/jdk_home。我将这个文件重命名为jdk_home.sh。旧的,现在我的路径不再被重写,我可以调用java -version并像预期的那样查看java 9。尽管我在update-aternative -config Java中正确地选择了Java 9,但是这个jdk_home。sh文件在启动时始终覆盖$PATH。

#8


0  

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

如果您想使用从Oracle站点下载的jdk,那么对我有用的(使用Mint)是使用更新替代方案:

  1. I downloaded the JDK and extracted it just anywhere, for example in /home/aqeel/development/jdk/jdk1.6.0_35
  2. 我下载了JDK,并在任何地方提取它,例如/home/aqeel/development/ JDK /jdk1.6.0_35
  3. I ran:

    我跑:

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

    Now you can execute sudo update-alternatives --config java and choose your java version.

    现在,您可以执行sudo更新选项—配置java并选择java版本。

  4. 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
  5. 这并没有设置我想要配置的JAVA_HOME变量,所以我将它添加到~/中。bashrc,包括一个导出JAVA_HOME="/home/aqeel/开发/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:

    为了根据所选的java替代方案动态地更改JAVA_HOME,我将这个代码片段添加到~/.bashrc中:

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

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 alias to your ~./bash_aliases:

    向您的~./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中取消与此相关的部分)

#9


-1  

simplest of all would be either to remove the link or edit /usr/bin/java. this has a call to execute the java at the mentioned path.

最简单的方法是删除链接或编辑/usr/bin/java它有一个在上述路径上执行java的调用。

#1


53  

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

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

  1. The output of env - specifically PATH.
  2. env的输出-特别是路径。
  3. which java tells you what?
  4. 哪个java告诉你什么?
  5. Is there a java executable in $JAVA_HOME\bin and does it have the execute bit set? If not chmod a+x java it.
  6. $JAVA_HOME\bin中是否有java可执行文件,它是否设置了执行位?如果不是chmod a+x java。

I trust you have source'd your .profile after adding/changing the JAVA_HOME and PATH?

我相信您在添加/更改了JAVA_HOME和PATH之后已经获得了.profile文件?

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

此外,您还可以通过编写日志来帮助您维护JDK安装

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

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

代替。然后,在安装JDK安装时,只需要更新一个env变量。

Cheers,

欢呼,

#2


11  

update-java-alternatives

update-java-alternatives

The java executable is not found with your JAVA_HOME, it only depends on your PATH.

java可执行文件在JAVA_HOME中没有找到,它只取决于您的路径。

update-java-alternatives is a good way to manage it for the entire system is through:

更新-java-alternative是管理整个系统的一个好方法:

update-java-alternatives -l

Sample output:

样例输出:

java-7-oracle 1 /usr/lib/jvm/java-7-oracle
java-8-oracle 2 /usr/lib/jvm/java-8-oracle

Choose one of the alternatives:

选择一种选择:

sudo update-java-alternatives -s java-7-oracle

Like update-alternatives, it works through symlink management. The advantage is that is manages symlinks to all the Java utilities at once: javac, java, javap, etc.

与更新替代方案一样,它通过符号链接管理工作。优点是可以同时管理到所有Java实用程序的符号链接:javac、Java、javap等等。

I am yet to see a JAVA_HOME effect on the JDK. So far, I have only seen it used in third-party tools, e.g. Maven.

我还没有看到JDK上的JAVA_HOME效果。到目前为止,我只在第三方工具(如Maven)中使用过。

#3


4  

$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,.

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

#4


2  

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.

例如路径= / 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]

检查:[哪个java]

you'll get the right one.

你会找到正确的。

#5


0  

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

有一个简单的方法,就是从“/usr/bin”中删除符号链接。它将工作。

#6


0  

Updating the ~/.profile or ~/.bash_profile does not work sometimes. I just deleted JDK 6 and sourced .bash_profile.

更新~ /。配置文件或~ /。bash_profile有时不工作。我刚刚删除了JDK 6并引用了。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和路径变量。

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

#7


0  

In Linux Mint 18 Cinnamon be sure to check /etc/profile.d/jdk_home.sh I 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 java this jdk_home.sh file kept overriding the $PATH on boot-up.

在Linux Mint 18中,一定要检查/etc/profile.d/jdk_home。我将这个文件重命名为jdk_home.sh。旧的,现在我的路径不再被重写,我可以调用java -version并像预期的那样查看java 9。尽管我在update-aternative -config Java中正确地选择了Java 9,但是这个jdk_home。sh文件在启动时始终覆盖$PATH。

#8


0  

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

如果您想使用从Oracle站点下载的jdk,那么对我有用的(使用Mint)是使用更新替代方案:

  1. I downloaded the JDK and extracted it just anywhere, for example in /home/aqeel/development/jdk/jdk1.6.0_35
  2. 我下载了JDK,并在任何地方提取它,例如/home/aqeel/development/ JDK /jdk1.6.0_35
  3. I ran:

    我跑:

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

    Now you can execute sudo update-alternatives --config java and choose your java version.

    现在,您可以执行sudo更新选项—配置java并选择java版本。

  4. 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
  5. 这并没有设置我想要配置的JAVA_HOME变量,所以我将它添加到~/中。bashrc,包括一个导出JAVA_HOME="/home/aqeel/开发/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:

    为了根据所选的java替代方案动态地更改JAVA_HOME,我将这个代码片段添加到~/.bashrc中:

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

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 alias to your ~./bash_aliases:

    向您的~./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中取消与此相关的部分)

#9


-1  

simplest of all would be either to remove the link or edit /usr/bin/java. this has a call to execute the java at the mentioned path.

最简单的方法是删除链接或编辑/usr/bin/java它有一个在上述路径上执行java的调用。