使 $JAVA_HOME 在 Ubuntu 中轻松更改

发布于 2024-08-29 08:32:13 字数 1459 浏览 17 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

别忘他 2024-09-05 08:32:13

将环境变量放入全局 /etc/environment 文件中:

...
export JAVA_HOME=/usr/lib/jvm/java-1.5.0-sun
...

在要更新变量的每个 shell 中执行“source /etc/environment”:

$ source /etc/environment

检查它是否有效:

$ echo $JAVA_HOME
$ /usr/lib/jvm/java-1.5.0-sun

很好,无需注销。

如果您只想在终端中设置 JAVA_HOME 环境变量,请在 ~/.bashrc 文件中设置它。

Put the environment variables into the global /etc/environment file:

...
export JAVA_HOME=/usr/lib/jvm/java-1.5.0-sun
...

Execute "source /etc/environment" in every shell where you want the variables to be updated:

$ source /etc/environment

Check that it works:

$ echo $JAVA_HOME
$ /usr/lib/jvm/java-1.5.0-sun

Great, no logout needed.

If you want to set JAVA_HOME environment variable in only the terminal, set it in ~/.bashrc file.

黎夕旧梦 2024-09-05 08:32:13

这可能会解决您的问题:
https://help.ubuntu.com/community/EnvironmentVariables

会话范围的环境变量

为了以影响特定用户环境的方式设置环境变量,不应在用户主目录中的特定 shell 脚本文件中放置命令来设置其值,而应使用:

~/.pam_environment - 该文件专门用于设置用户环境。它不是一个脚本文件,而是由赋值表达式组成,每行一个。

不推荐:

~/.profile - 这可能是放置环境变量分配的最佳文件,因为它在启动过程桌面会话期间由 DisplayManager 自动执行,并且在从文本登录时由登录 shell 自动执行。控制台。

This will probably solve your problem:
https://help.ubuntu.com/community/EnvironmentVariables

Session-wide environment variables

In order to set environment variables in a way that affects a particular user's environment, one should not place commands to set their values in particular shell script files in the user's home directory, but use:

~/.pam_environment - This file is specifically meant for setting a user's environment. It is not a script file, but rather consists of assignment expressions, one per line.

Not recommended:

~/.profile - This is probably the best file for placing environment variable assignments in, since it gets executed automatically by the DisplayManager during the startup process desktop session as well as by the login shell when one logs-in from the textual console.

心病无药医 2024-09-05 08:32:13

尝试这些步骤。

--我们要编辑“etc\profile”。
环境变量将在文件底部输入。由于 Ubuntu 没有
授予对根文件夹的访问权限,我们必须在终端中使用一些命令

第 1 步:启动终端。输入命令:gksudo gedit /etc/profile

第 2 步:配置文件文本文件将打开。在页面底部输入环境变量............ 例如: export JAVA_HOME=/home/alex/jdk1.6.0_22/bin/java< /code>

export PATH=/home/alex/jdk1.6.0_22/bin:$PATH

步骤3:保存并关闭文件。使用echo命令检查环境变量是否设置...... Eg echo $PATH

Try these steps.

--We are going to edit "etc\profile".
The environment variables are to be input at the bottom of the file. Since Ubuntu does not
give access to root folder, we will have to use a few commands in the terminal

Step1: Start Terminal. Type in command: gksudo gedit /etc/profile

Step2: The profile text file will open. Enter the environment variables at the bottom of the page........... Eg: export JAVA_HOME=/home/alex/jdk1.6.0_22/bin/java

export PATH=/home/alex/jdk1.6.0_22/bin:$PATH

step3: save and close the file. Check if the environment variables are set by using echo command........ Eg echo $PATH

幼儿园老大 2024-09-05 08:32:13

您需要将变量定义放在 ~/.bashrc 文件中。

来自 bash 手册页:

当交互式 shell 是
未启动登录 shell,bash
读取并执行命令
/etc/bash.bashrc 和 ~/.bashrc,如果
这些文件存在。

You need to put variable definition in the ~/.bashrc file.

From bash man page:

When an interactive shell that is
not a login shell is started, bash
reads and executes commands from
/etc/bash.bashrc and ~/.bashrc, if
these files exist.

为人所爱 2024-09-05 08:32:13

传统上,如果您只想更改终端窗口中的变量,请将其设置在 .bashrc 文件中,每次打开新终端时都会获取该文件。每次打开新终端时,不会获取 .profile 文件。

查看相关 .profile 和 .bashrc 之间的区别:
.bashrc、.bash_profile 和 .environment 之间有什么区别?

.bashrc 应该可以解决您的问题。然而,这不是正确的解决方案,因为您使用的是 Ubuntu。请参阅相关的 Ubuntu 帮助页面“会话范围的环境变量”。因此,难怪 .profile 不适合您。我使用 Ubuntu 12.04 和 xfce。我设置了我的 .profile,即使我注销并登录,它也根本没有生效。类似的体验 此处。因此,您可能必须使用 .pam_environment 文件,并完全忘记 .profile.bashrc。请注意,.pam_environment 不是脚本文件。

Traditionally, if you only want to change the variable in your terminal windows, set it in .bashrc file, which is sourced each time a new terminal is opened. .profile file is not sourced each time you open a new terminal.

See the difference between .profile and .bashrc in question:
What's the difference between .bashrc, .bash_profile, and .environment?

.bashrc should solve your problem. However, it is not the proper solution since you are using Ubuntu. See the relevant Ubuntu help page "Session-wide environment variables". Thus, no wonder that .profile does not work for you. I use Ubuntu 12.04 and xfce. I set up my .profile and it is simply not taking effect even if I log out and in. Similar experience here. So you may have to use .pam_environment file and totally forget about .profile, and .bashrc. And NOTE that .pam_environment is not a script file.

欢烬 2024-09-05 08:32:13

看一下bash(1),你需要登录 shell 用于获取 ~/.profile,即 -l 选项。

Take a look at bash(1), you need a login shell to pickup the ~/.profile, i.e. the -l option.

冷弦 2024-09-05 08:32:13

我知道这是一个很长的冷问题,但每次有新的或最近的主要 Java 版本发布时都会出现这个问题。现在这很容易适用于 6 和 7 的交换。

我过去曾使用 update-java-alternatives 完成此操作:
http://manpages.ubuntu.com/manpages/hardy /man8/update-java-alternatives.8.html

I know this is a long cold question, but it comes up every time there is a new or recent major Java release. Now this would easily apply to 6 and 7 swapping.

I have done this in the past with update-java-alternatives:
http://manpages.ubuntu.com/manpages/hardy/man8/update-java-alternatives.8.html

戴着白色围巾的女孩 2024-09-05 08:32:13

对 .profile 进行更改后,您需要执行该文件以使更改生效。

root@masternode# . ~/.profile

完成此操作后,echo 命令将起作用。

After making changes to .profile, you need to execute the file, in order for the changes to take effect.

root@masternode# . ~/.profile

Once this is done, the echo command will work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文