关于在Centos中设置jdk、maven等参数不生效的解决方法

来源:互联网 发布:为啥女生干java反应慢 编辑:程序博客网 时间:2024/06/06 11:48

          在之前几天的一篇文章中提及到关于如何在Centos中安装jdk,在安装完之后我们需要在/etc/profile里面进行参数配置,然后使用source /etc/profile来使得命令生效。但是这里有一个问题就是当在终端退出账号,或者机器重启之后这个变量就不会生效。PATH的配置却没有被更新,其他部分的都已经有更新。

我们先来了解下Linux下面关于环境变量的优先级问题:

Linux 环境变量用户环境变量通常被存储在下面的文件中:

~/.profile

~/.bash_profile 或者 ~./bash_login

~/.bashrc

系统环境变量一般保存在下面的文件中:/etc/environment/etc/profile/etc/bashrcUbuntu Linux 

环境变量的优先级

1./etc/profile:在登录时,操作系统定制用户环境时使用的第一个文件,此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件自动被执行。

2./etc/environment:在登录时操作系统使用的第二个文件,系统在读取你自己的profile前,设置环境文件的环境变量。3.~/.bash_profile:在登录时用到的第三个文件是.profile文件,每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc文件。/etc/bashrc:为每一个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取.~/.bashrc:该文件包含专用于你的bash shell的bash信息,当登录时以及每次打开新的shell时,该该文件被读取。/etc/bashrc:当用户进入shell或桌面系统自动执行的脚本

几个环境变量的优先级:1>2>3

然后我们看看3里面的脚本内容,如下图所示,在最后他会去读取/etc/bashrc 这个脚本里面的内容


然后看看bashrc的代码 


# /etc/bashrc# System wide functions and aliases# Environment stuff goes in /etc/profile# It's NOT a good idea to change this file unless you know what you# are doing. It's much better to create a custom.sh shell script in# /etc/profile.d/ to make custom changes to your environment, as this# will prevent the need for merging in future updates.# are we an interactive shell?if [ "$PS1" ]; then  if [ -z "$PROMPT_COMMAND" ]; then    case $TERM in    xterm*|vte*)      if [ -e /etc/sysconfig/bash-prompt-xterm ]; then          PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm      elif [ "${VTE_VERSION:-0}" -ge 3405 ]; then          PROMPT_COMMAND="__vte_prompt_command"      else          PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'      fi      ;;    screen*)      if [ -e /etc/sysconfig/bash-prompt-screen ]; then          PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen      else          PROMPT_COMMAND='printf "\033k%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'      fi      ;;    *)      [ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default      ;;    esac  fi  # Turn on parallel history  shopt -s histappend  history -a  # Turn on checkwinsize  shopt -s checkwinsize  [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "  # You might want to have e.g. tty in prompt (e.g. more virtual machines)  # and console windows  # If you want to do so, just add e.g.  # if [ "$PS1" ]; then  #   PS1="[\u@\h:\l \W]\\$ "  # fi  # to your custom modification shell script in /etc/profile.d/ directoryfiif ! shopt -q login_shell ; then # We're not a login shell    # Need to redefine pathmunge, it get's undefined at the end of /etc/profile    pathmunge () {        case ":${PATH}:" in            *:"$1":*)                ;;            *)                if [ "$2" = "after" ] ; then                    PATH=$PATH:$1                else                    PATH=$1:$PATH                fi        esac    }    # By default, we want umask to get set. This sets it for non-login shell.    # Current threshold for system reserved uid/gids is 200    # You could check uidgid reservation validity in    # /usr/share/doc/setup-*/uidgid file    if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then       umask 002    else       umask 022    fi    SHELL=/bin/bash    # Only display echos from profile.d scripts if we are no login shell    # and interactive - otherwise just process them to set envvars    for i in /etc/profile.d/*.sh; do        if [ -r "$i" ]; then            if [ "$PS1" ]; then                . "$i"            else                . "$i" >/dev/null            fi        fi    done    unset i    unset -f pathmungefi# vim:ts=4:sw=4export HISTSIZE=3000export HISTTIMEFORMAT="%F %T "export PROMPT_COMMAND="history -a"unset HISTCONTROLexport PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/java/jdk1.8.0_131/bin:/root/bin:/usr/local/git/bin

可以看到在最后是输入一个固定的PATH变量。问题的原因就在这里。

解决方法:

1、把java的环境变量配置在/etc/bashrc底部,这样子每次输出都可以读取到

2、把java的环境变量配置放在/etc/profile.d 文件夹里面,以.sh结尾的脚本文件,在/etc/bashrc中的最后PATH中增加:$PATH这个配置,把配置串联起来,这样子就算机器重启也不会把PATH的变量丢失了。

参考文章:

http://carrie1314.blog.51cto.com/6634326/1296298

阅读全文
0 0