简体   繁体   English

登录时 ~/bash_profile 中的 $JAVA_HOME 路径来源不正确

[英]$JAVA_HOME path not sourced correctly in ~/bash_profile at login

Upon logging into my CentOS 7 VM, my $JAVA_HOME is always set to: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-11.b12.el7.x86_64/jre , which is incorrect.登录我的 CentOS 7 VM 后,我的 $JAVA_HOME 始终设置为: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.131-11.b12.el7.x86_64/jre ,这是不正确的。

My ~/.bash_profile reads:我的 ~/.bash_profile 是这样写的:

#  .bash_profile

#  Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

# User specific environment and startup programs
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.144-0.b01.el7_4.x86_64/jre
export JAVA_HOME

PYCHARM_HOME=/opt/pycharm-community-2017.2.3/
export PYCHARM_HOME

ECLIPSE_HOME=/opt/eclipse
export ECLIPSE_HOME

export SPARK_HOME=/opt/spark

PATH=$PATH:$HOME/.local/bin:$HOME/bin:$JAVA_HOME/bin:$SPARK_HOME/bin:$PYCHARM_HOME/bin:$ECLIPSE_HOME

export PATH

Sourcing.bash_profile each time I open a new terminal appears to correct the issue, but why is my $JAVA_HOME path defaulting to an old version on startup and not being set to the directory specified in.bash_profile?每次我打开一个新终端时,Sourcing.bash_profile 似乎都可以解决问题,但为什么我的 $JAVA_HOME 路径在启动时默认为旧版本并且没有设置为在 .bash_profile 中指定的目录?

  1. Run ps in the interactive shell to make sure you're in bash . 在交互式shell中运行ps以确保您处于bash
  2. Try bash -l after login and see if it fixes your problem. 登录后尝试使用bash -l ,看看它是否可以解决您的问题。
  3. Add set -x before JAVA_HOME=... to check if there's something wrong. JAVA_HOME=...之前添加set -x以检查是否有问题。

Sourcing .bash_profile each time I open a new terminal appears to correct the issue, but why is my $JAVA_HOME path defaulting to an old version on startup and not being set to the directory specified in .bash_profile? 每次打开新终端时都要采购.bash_profile似乎可以解决此问题,但是为什么我的$ JAVA_HOME路径在启动时默认为旧版本,而不设置为.bash_profile中指定的目录?

You need to logout from current user and login again so environment variables changes take place. 您需要从当前用户注销并再次登录,以便进行环境变量更改。

May be useful 可能有用

In Bash: 在Bash中:

  • Bash as login shell will load /etc/profile , ~/.bash_profile , ~/.bash_login , ~/.profile in the order. Bash作为登录shell将按顺序加载/etc/profile~/.bash_profile~/.bash_login~/.profile
  • Bash as non-login interactive shell will load ~/.bashrc Bash作为非登录交互shell将加载~/.bashrc

Environment : 环境 :

  • Use /etc/environment to permanently system wide (all users, all processes) set environmental variables for all users. 使用/etc/environment可以在整个系统范围内(所有用户,所有进程)永久设置所有用户的环境变量。

    /etc/environment is a system-wide configuration file, which means it is used by all users. /etc/environment是系统范围的配置文件,表示所有用户都使用它。 It is owned by root though, so you need to be an admin user and use sudo to modify it. 它是由root拥有的,因此您需要成为admin用户并使用sudo对其进行修改。

    Suppose if you set foo="bar" variable foo will be accessible from all the user sessions. 假设如果设置了foo="bar"变量,那么可以从所有用户会话访问foo To test the variable output first source it 首先测试变量输出

    source /etc/environment

  • ~/.profile is one of your own user's personal shell initialization scripts. ~/.profile是您自己用户的个人shell初始化脚本之一。 Every user has one and can edit their file without affecting others. 每个用户都有一个文件,可以编辑其文件而不会影响其他文件。

  • /etc/profile and /etc/profile.d/*.sh are the global initialization scripts that are equivalent to ~/.profile for each user. /etc/profile/etc/profile.d/*.sh是全局初始化脚本,等效于每个用户~/.profile The global scripts get executed before the user-specific scripts though; 但是,全局脚本在用户特定脚本之前执行; and the main /etc/profile executes all the *.sh scripts in /etc/profile.d/ just before it exits. 并且主/etc/profile在退出之前执行/etc/profile.d/所有*.sh脚本。

Also note, 另请注意,

  • The /etc/environment file sets the variable system wide for every user on login. /etc/environment文件为每个登录用户设置系统范围的变量。
  • Commands in the bash_profile are is executed if the bash shell is opened by any user. 如果bash shell由任何用户打开,则执行bash_profile中的命令。 So the variables would not be set unless a bash shell is opened at least one time. 因此,除非至少打开一次bash shell,否则将不会设置变量。

It worked for me!它对我有用!

In my case, The follwing caused the error:就我而言,以下原因导致了错误:

export JAVA_HOME = /Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home

The error was that there were spaces before and after the = .错误是=前后有空格 The error disappeared after those spaces were removed.删除这些空格后错误消失。

So the correct command is as follows:所以正确的命令如下:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM