一 已有jdk卸载
1.whereis java
2.which java
3.sudo rm -rf /usr/local/jdk1.8.0_131
4.sudo gedit /etc/profile
删除代码:
#set java environment
export JAVA_HOME=/opt/jdk1.6.0_20
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JRE_HOME/lib
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
保存退出。
二 安装和切换JDK
1.官网下载JDK
2. 解压缩,放到指定目录
sudo mkdir /usr/lib/jvm
sudo tar -zxvf ~/下载/jdk-7u79-linux-x64.tar.gz -C /usr/lib/jvm
3.修改环境变量:
sudo vim ~/.bashrc
文件的末尾追加下面内容:
#set oracle jdk environment
export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_60 ## 这里要注意目录要换成自己解压的jdk 目录
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
使环境变量马上生效
source ~/.bashrc
4.设置系统默认jdk 版本
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.7.0_79/bin/java 300
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.7.0_79/bin/javac 300
sudo update-alternatives --install /usr/bin/jar jar /usr/lib/jvm/jdk1.7.0_79/bin/jar 300
sudo update-alternatives --install /usr/bin/javah javah /usr/lib/jvm/jdk1.7.0_79/bin/javah 300
sudo update-alternatives --install /usr/bin/javap javap /usr/lib/jvm/jdk1.7.0_79/bin/javap 300
然后执行:
sudo update-alternatives --config java
若是初次安装jdk,会有下面的提示
There is only one alternative in link group java (providing /usr/bin/java):
/usr/lib/jvm/jdk1.7.0_60/bin/java
否者,选择合适的jdk
5.测试jdk
java -version