Linux 如何在 Ubuntu 上设置 JAVA_HOME 路径?

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

How to set JAVA_HOME path on Ubuntu?

javalinuxubuntujava-home

提问by MChan

How can I setup JAVA_HOMEpath without having to set it each time I restart my machine?

如何设置JAVA_HOME路径而不必每次重新启动机器时都设置它?

I've used the following ways when trying to set JAVA_HOME on my Ubuntu machine:

尝试在我的 Ubuntu 机器上设置 JAVA_HOME 时,我使用了以下方法:

1) From terminal I've executed the following command:

1)从终端我执行了以下命令:

export JAVA_HOME=/usr/lib/jvm/jdk1.7.0

2) I've edited the /etc/enviromentfile directly to add JAVA_HOMEpath in it

2)我/etc/enviroment直接编辑了文件以JAVA_HOME在其中添加路径

What's really strange is that if I test JAVA_HOMEusing the echocommand after an of the above two ways, I can see it is set correctly, but if I restart, logout/ login again or even after working on the machine for a while the JAVA_HOMEis no more set and I have to set it again using any of the above two ways.

真正奇怪的是,如果我在上述两种方式中的一种之后JAVA_HOME使用该echo命令进行测试,我可以看到它设置正确,但是如果我重新启动,注销/再次登录,甚至在机器上工作一段时间后,它就JAVA_HOME不再存在了设置,我必须使用上述两种方式中的任何一种重新设置它。

So can someone please tell me what I am doing wrong here?

那么有人可以告诉我我在这里做错了什么吗?

采纳答案by Nasir

I normally set paths in

我通常设置路径

~/.bashrc

However for Java, I followed instructions at https://askubuntu.com/questions/55848/how-do-i-install-oracle-java-jdk-7

但是对于 Java,我按照https://askubuntu.com/questions/55848/how-do-i-install-oracle-java-jdk-7 上的说明进行操作

and it was sufficient for me.

这对我来说已经足够了。

you can also define multiple java_home's and have only one of them active (rest commented).

您还可以定义多个 java_home 并且只有其中一个处于活动状态(其余评论)。

suppose in your bashrc file, you have

假设在你的 bashrc 文件中,你有

export JAVA_HOME=......jdk1.7

export JAVA_HOME=......jdk1.7

#export JAVA_HOME=......jdk1.8

#export JAVA_HOME=......jdk1.8

notice 1.8 is commented. Once you do

注释 1.8 已注释。一旦你这样做

source ~/.bashrc

source ~/.bashrc

jdk1.7 will be in path.

jdk1.7 将在路径中。

you can switch them fairly easily this way. There are other more permanent solutions too. The link I posted has that info.

您可以通过这种方式轻松切换它们。还有其他更持久的解决方案。我发布的链接有这些信息。

回答by morgano

add JAVA_HOME to the file:

将 JAVA_HOME 添加到文件中:

/etc/environment

for it to be available to the entire system (you would need to restart Ubuntu though)

使其可用于整个系统(尽管您需要重新启动 Ubuntu)