关于在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 这个脚本里面的内容
# /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
- 关于在Centos中设置jdk、maven等参数不生效的解决方法
- 关于Servlet中@WebServlet注释不生效的解决方法
- 关于mybatis中gruop参数不生效
- 一个电脑上同时安装多个版本JDK,设置环境变量不生效的解决方法
- CentOS 6.4设置静态IP不生效解决方法
- 关于安装版JDK1.8 1.7更改JDK环境变量不生效的解决方法
- 通过setup改centOS ip不生效的解决方法
- maven在eclipse中配置过程并导入maven工程,以及关于工程中jdk和git的设置,
- ngmodel在Eonasdan/bootstrap-datetimepicker中不生效解决方法
- 关于在TextView中设置DrawableLeft,DrawableRight等不显示的问题
- 关于UGUI中Button,InputField等不响应的解决方法
- 关于在maven中 遇到maven不加载的问题
- 关于HOSTS设置不生效的解决小方法
- 关于HOSTS设置不生效的解决小方法
- 关于极光IM设置昵称不生效的解决办法
- 项目中引入 Spring AOP 但不生效的解决方法
- 关于在Spring Cloud Feign工程中使用Ribbon配置不生效的问题
- 关于在Spring Cloud Feign工程中使用Hystrix配置不生效的问题
- 基于maven使用IDEA创建多模块项目
- eclipse之jrebel热部署插件
- linux对于特定文件夹删除
- <hr>分割线样式
- GitHub 使用markdown格式的三个坑
- 关于在Centos中设置jdk、maven等参数不生效的解决方法
- cvc-complex-type.2.3: 元素 'beans' 必须不含字符 [子级], 因为该类型的内容类型为“仅元素”。
- Activity和Service的交互方式
- Python学习笔记(1) -- Python内建函数文档
- 数据库范式笔记
- ul li 居中排列
- <fieldset>的样式
- 静态代码块,构造代码块,构造方法的执行顺序
- 170604 逆向-CrackMe之013