最近打算玩玩Jenkins这玩意,在虚拟机上搭建的ubuntu系统(非桌面版本),jdk早就安装好,奈何出现了一个问题影响了,鼓捣了我大半天,问题如下:

        在/etc/profile中设置的环境变量后,通过source /etc/profile后,一切现实OK,但是切换用户后,环境变量失效了,但是环境变量失效了,但是环境变量失效了(重要的事情说三遍),出现的问题如下:


程序 'java' 已包含在下列软件包中:
 * default-jre
 * gcj-4.9-jre-headless
 * gcj-5-jre-headless
 * openjdk-8-jre-headless
 * gcj-4.8-jre-headless
 * openjdk-9-jre-headless
请尝试:apt install <选定的软件包>


奈何重新source /etc/profile后,环境变量就好了。


分析问题原因:

      本人的环境:

       ubuntu版本:Ubuntu 17.04 \n \l

       jdk版本:java version "1.7.0_79"

       肯定是环境变量配置错误了,至于错在哪里,我不清楚,于是查找Ubuntu官网的资料。


官网中关于系统环境变量有如下分类:

1. /etc/environment

2. /etc/profile.d/*.sh

3. Other files

其中在Other files中有这样说道:

While /etc/profile is often suggested for setting environment variables system-wide, it is a configuration file of the base-files package, so it's not appropriate to edit that file directly. Use a file in /etc/profile.d instead as shown above. (Files in /etc/profile.d are sourced by /etc/profile.)

/etc/default/locale is specifically meant for system-wide locale environment variable settings. It's written to by the installer and when you use Language Support to set the language or regional formats system-wide. On a desktop system there is normally no reason to edit this file manually.

The shell config file /etc/bash.bashrc is sometimes suggested for setting environment variables system-wide. While this may work on Bash shells for programs started from the shell, variables set in that file are not available by default to programs started from the graphical environment in a desktop session.


/ect/profile是经常被设置为系统环境变量的,但是不希望直接被编辑,使用/etc/profile.d代替。总结就是/etc/profile是不被被推荐使用作为环境变量的

ubuntu官网环境变量说明


那如何配置jdk的环境变量呢?

     1.解压jdk-8u131-linux-x64.tar.gz

              sudo tar -zxvf jdk-8u131-linux-x64.tar.gz -C  /usr/local

      2.设置环境变量

             2.1 在/etc/environment中编辑如下:

JAVA_HOME="/usr/local/jdk1.7.0_79"
CLASSPATH=".:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar"
PATH="$JAVA_HOME/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"

注意:PATH中默认的值一个都不要删了,在原有的基础上面添加JAVA_HOME即可!

             2.2 使用source命令使其生效

                   source /etc/environment

      3.Ubuntu系统默认安装并使用OpenJDK,因此需要手动修改系统默认的JDK

                

sudo update-alternatives --install /usr/bin/java java /usr/local/jdk1.7.0_79/bin/java 300

sudo update-alternatives --install /usr/bin/javac javac /usr/local/jdk1.7.0_79/bin/javac 300

sudo update-alternatives --config java
sudo update-alternatives --config javac

注意:后面的java/javac路径是你在linux中安装java源文件的绝对路径

至此配置完成,输入java -version、javac或java检查是否配置成功。



Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐