Linux 在 Ubuntu 中轻松更改 $JAVA_HOME
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2655641/
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
Make $JAVA_HOME easily changable in Ubuntu
提问by Junho Park
In Ubuntu, I'd like to switch my JAVA_HOME environment variable back and forth between Java 5 and 6.
在 Ubuntu 中,我想在 Java 5 和 6 之间来回切换我的 JAVA_HOME 环境变量。
I open a terminal and type in the following to set the JAVA_HOME environment variable:
我打开一个终端并输入以下内容来设置 JAVA_HOME 环境变量:
export JAVA_HOME=/usr/lib/jvm/java-1.5.0-sun
And in that same terminal window, I type the following to check that the environment variable has been updated:
在同一个终端窗口中,我键入以下内容以检查环境变量是否已更新:
echo $JAVA_HOME
And I see /usr/lib/jvm/java-1.5.0-sunwhich is what I'm expecting to see. In addition, I modify ~/.profile and set the JAVA_HOME environment variable to /usr/lib/jvm/java-1.5.0-sun.
我看到/usr/lib/jvm/java-1.5.0-sun这是我期待看到的。另外我修改~/.profile,将JAVA_HOME环境变量设置为/usr/lib/jvm/java-1.5.0-sun。
And now for the problem--when I open a new terminal window and I check my JAVA_HOME environment variable by typing in echo $JAVA_HOMEI see that my JAVA_HOME environment variable has been reverted back to Java 6. When I reboot my machine (or log out and back in, I suppose) the JAVA_HOME environment variable is set to Java 5 (presumably because of the modification I made in my ~/.profile).
现在问题来了——当我打开一个新的终端窗口并通过输入echo $JAVA_HOME检查我的 JAVA_HOME 环境变量时,我看到我的 JAVA_HOME 环境变量已恢复为 Java 6。当我重新启动我的机器(或登录我想是退出并返回)JAVA_HOME 环境变量设置为 Java 5(大概是因为我在 ~/.profile 中进行了修改)。
Is there a way around this so that I can change my JAVA_HOME environment without having to log out and back in (AND make that environment variable change stickin all new terminal windows)?
有没有办法解决这个问题,这样我就可以更改我的 JAVA_HOME 环境而不必注销并重新登录(并使该环境变量更改坚持所有新的终端窗口)?
采纳答案by Martin Konicek
Put the environment variables into the global /etc/environment
file:
将环境变量放入全局/etc/environment
文件中:
...
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:
在要更新变量的每个 shell 中执行“source /etc/environment”:
$ 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 ~/.bashrcfile.
如果您只想在终端中设置 JAVA_HOME 环境变量,请将其设置在~/.bashrc文件中。
回答by Nikolai Fetissov
回答by Holger Frohloff
This will probably solve your problem: https://help.ubuntu.com/community/EnvironmentVariables
这可能会解决您的问题: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.
会话范围的环境变量
为了以影响特定用户环境的方式设置环境变量,不应在用户主目录中的特定 shell 脚本文件中放置命令来设置其值,而应使用:
~/.pam_environment - 该文件专门用于设置用户的环境。它不是一个脚本文件,而是由赋值表达式组成,每行一个。
不建议:
~/.profile - 这可能是放置环境变量分配的最佳文件,因为它在启动过程桌面会话期间由 DisplayManager 自动执行,并且在从文本控制台登录时由登录 shell 自动执行。
回答by Alex
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
--我们将编辑“etc\profile”。环境变量将在文件底部输入。由于 Ubuntu 不授予访问根文件夹的权限,我们将不得不在终端中使用一些命令
Step1: Start Terminal. Type in command: gksudo gedit /etc/profile
第一步:启动终端。输入命令: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
步骤 2:配置文件文本文件将打开。在页面底部输入环境变量.......例如: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........ Egecho $PATH
step3:保存并关闭文件。使用echo命令检查是否设置了环境变量......例如echo $PATH
回答by Pawe? Nadolski
You need to put variable definition in the ~/.bashrc
file.
您需要将变量定义放在~/.bashrc
文件中。
From bash man page:
从 bash 手册页:
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.
当一个不是登录 shell 的交互式 shell 启动时,bash 会从 /etc/bash.bashrc 和 ~/.bashrc 读取并执行命令,如果这些文件存在的话。
回答by Alain O'Dea
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.
我知道这是一个很长很冷的问题,但每次有新的或最近的主要 Java 版本时都会出现。现在这很容易适用于 6 和 7 交换。
I have done this in the past with update-java-alternatives
:
http://manpages.ubuntu.com/manpages/hardy/man8/update-java-alternatives.8.html
我过去曾这样做过update-java-alternatives
:http:
//manpages.ubuntu.com/manpages/hardy/man8/update-java-alternatives.8.html
回答by HongboZhu
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.
传统上,如果您只想更改终端窗口中的变量,请将其设置在.bashrc
文件中,每次打开新终端时都会提供该文件。.profile
每次打开新终端时都不会获取文件。
See the difference between .profile and .bashrc in question: What's the difference between .bashrc, .bash_profile, and .environment?
请参阅有问题的 .profile 和 .bashrc 之间的区别: .bashrc、.bash_profile 和 .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.
.bashrc
应该可以解决您的问题。但是,这不是正确的解决方案,因为您使用的是 Ubuntu。请参阅相关的 Ubuntu 帮助页面“会话范围的环境变量”。因此,难怪这.profile
对您不起作用。我使用 Ubuntu 12.04 和 xfce。我设置了我的.profile
,即使我注销并登录它也根本不生效。类似的经验在这里。所以你可能不得不使用.pam_environment
file 而完全忘记.profile
, 和.bashrc
。并注意这.pam_environment
不是脚本文件。
回答by HongboZhu
After making changes to .profile, you need to execute the file, in order for the changes to take effect.
对 .profile 进行更改后,您需要执行该文件,以使更改生效。
root@masternode# . ~/.profile
Once this is done, the echo command will work.
完成后,echo 命令将起作用。