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

↘锁芯ラ 提交于 2019-12-23 02:27:10

问题


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.

My ~/.bash_profile reads:

#  .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?


回答1:


  1. Run ps in the interactive shell to make sure you're in bash.
  2. Try bash -l after login and see if it fixes your problem.
  3. Add set -x before JAVA_HOME=... to check if there's something wrong.



回答2:


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?

You need to logout from current user and login again so environment variables changes take place.

May be useful

In Bash:

  • Bash as login shell will load /etc/profile, ~/.bash_profile, ~/.bash_login, ~/.profile in the order.
  • Bash as non-login interactive shell will load ~/.bashrc

Environment :

  • Use /etc/environment to permanently system wide (all users, all processes) set environmental variables for all users.

    /etc/environment is a system-wide configuration file, which means it is used by all users. It is owned by root though, so you need to be an admin user and use sudo to modify it.

    Suppose if you set foo="bar" variable foo will be accessible from all the user sessions. To test the variable output first source it

    source /etc/environment

  • ~/.profile is one of your own user's personal shell initialization scripts. 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. 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.

Also note,

  • The /etc/environment file sets the variable system wide for every user on login.
  • Commands in the bash_profile are is executed if the bash shell is opened by any user. So the variables would not be set unless a bash shell is opened at least one time.


来源:https://stackoverflow.com/questions/46679363/java-home-path-not-sourced-correctly-in-bash-profile-at-login

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!