在Ubuntu上设置JAVA_HOME路径。

时间:2021-08-21 23:06:16

How can I setup JAVA_HOME path 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/enviroment file directly to add JAVA_HOME path in it

2)我已经编辑了/etc/ environmentoment文件,以便在其中添加JAVA_HOME路径

What's really strange is that if I test JAVA_HOME using the echo command 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_HOME is 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?

有人能告诉我我在这里做错了什么吗?

2 个解决方案

#1


38  

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,并且只有一个是活动的(rest注释)。

suppose in your bashrc file, you have

假设在bashrc文件中有。

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

出口JAVA_HOME =……jdk1.7

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

#出口JAVA_HOME =……jdk1.8

notice 1.8 is commented. Once you do

注意1.8是评论。一旦你做

source ~/.bashrc

源~ / . 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.

你可以用这种方式很容易地切换它们。还有其他更持久的解决办法。我发布的链接上有这个信息。

#2


43  

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)

#1


38  

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,并且只有一个是活动的(rest注释)。

suppose in your bashrc file, you have

假设在bashrc文件中有。

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

出口JAVA_HOME =……jdk1.7

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

#出口JAVA_HOME =……jdk1.8

notice 1.8 is commented. Once you do

注意1.8是评论。一旦你做

source ~/.bashrc

源~ / . 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.

你可以用这种方式很容易地切换它们。还有其他更持久的解决办法。我发布的链接上有这个信息。

#2


43  

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)