环境:Linux内核版本4.4.59+、jdk1.8安装、Debian9系统
注意:
1、本文介绍的是在Debian中使用jdk的rpm包进行安装
JDK完全卸载(需要在root模式下进行操作)
切换到root模式下,或者使用sudo 执行命令
1、移除所有 Java相关包 (Sun, Oracle, OpenJDK, IcedTea plugins, GIJ)
sudo apt-cache search java | awk '{print($1)}' | grep -E -e '^(ia32-)?(sun|oracle)-java' -e '^openjdk-' -e '^icedtea' -e '^(default|gcj)-j(re|dk)' -e '^gcj-(.*)-j(re|dk)' -e 'java-common' | xargs sudo apt-get -y remove
sudo apt-get -y autoremove
进行更新
sudo apt-get update
2、清除配置信息:
sudo dpkg -l | grep ^rc | awk '{print($2)}' | xargs sudo apt-get -y purge
3、清除java配置及缓存:
sudo bash -c 'ls -d /home/*/.java' | xargs sudo rm -rf
4、清除jvm文件夹里面的文件
sudo rm -rf /usr/lib/jvm/*
JDK安装
点击进入Oracle JDK下载官网
1、下载JDK,Debian/Ubuntu系统直接下载后缀名为deb文件的更加方便(若没有deb的后缀名文件下载tar.gz压缩包也一样)。如果都没有也可以下载rpm包,不过需要转换成deb包之后才能在Debian/Ubuntu中使用。
2、 后缀名为deb的文件下载完成之后可参考第4步,直接使用dpkg命令执行,tar.gz的压缩包下载完成之后需要进行解压缩安装
注意:若不能复制或是创建目录,则需要切换到root用户或者使用sudo命令执行;
3、我使用的是之前下载好的rpm包,直接使用rpm -ivh报错:
coder@codercom-code-server1:~/temp$ sudo rpm -ivh jdk-8u221-linux-x64.rpm
rpm: RPM should not be used directly install RPM packages, use Alien instead!
rpm: However assuming you know what you are doing...
warning: jdk-8u221-linux-x64.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY
error: Failed dependencies:
/bin/basename is needed by jdk1.8-2000:1.8.0_221-fcs.x86_64
/bin/cat is needed by jdk1.8-2000:1.8.0_221-fcs.x86_64
/bin/cp is needed by jdk1.8-2000:1.8.0_221-fcs.x86_64
/bin/gawk is needed by jdk1.8-2000:1.8.0_221-fcs.x86_64
/bin/grep is needed by jdk1.8-2000:1.8.0_221-fcs.x86_64
/bin/ln is needed by jdk1.8-2000:1.8.0_221-fcs.x86_64
/bin/ls is needed by jdk1.8-2000:1.8.0_221-fcs.x86_64
/bin/mkdir is needed by jdk1.8-2000:1.8.0_221-fcs.x86_64
/bin/mv is needed by jdk1.8-2000:1.8.0_221-fcs.x86_64
/bin/pwd is needed by jdk1.8-2000:1.8.0_221-fcs.x86_64
/bin/rm is needed by jdk1.8-2000:1.8.0_221-fcs.x86_64
/bin/sed is needed by jdk1.8-2000:1.8.0_221-fcs.x86_64
/bin/sort is needed by jdk1.8-2000:1.8.0_221-fcs.x86_64
/bin/touch is needed by jdk1.8-2000:1.8.0_221-fcs.x86_64
/usr/bin/cut is needed by jdk1.8-2000:1.8.0_221-fcs.x86_64
/usr/bin/dirname is needed by jdk1.8-2000:1.8.0_221-fcs.x86_64
/usr/bin/expr is needed by jdk1.8-2000:1.8.0_221-fcs.x86_64
/usr/bin/find is needed by jdk1.8-2000:1.8.0_221-fcs.x86_64
/usr/bin/tail is needed by jdk1.8-2000:1.8.0_221-fcs.x86_64
/usr/bin/tr is needed by jdk1.8-2000:1.8.0_221-fcs.x86_64
/usr/bin/wc is needed by jdk1.8-2000:1.8.0_221-fcs.x86_64
/bin/sh is needed by jdk1.8-2000:1.8.0_221-fcs.x86_64
coder@codercom-code-server1:~/temp$
原因是Debian不支持rpm命令了, 其软件以deb包或者源文件的形式出现。于是需要把rpm转化成deb。
安装alien:
sudo apt-get update
sudo apt-get install alien
rpm转换成deb:
alien -d jdk-8u221-linux-x64.rpm
下面是转换过程和转换完成后的文件列表,转换过程中alien会创建一个文件夹用来存放解压后的rpm包数据,然后再重新打成deb包,新建的文件夹在deb包完成后会自动删除:
4、deb转换完成后执行安装命令(或直接下载deb包,然后使用以下命令进行安装,文件名请自行替换):
coder@codercom-code-server1:~/temp$ sudo dpkg -i jdk1.8_1.8.0221-1_amd64.deb
Selecting previously unselected package jdk1.8.
(Reading database ... 32734 files and directories currently installed.)
Preparing to unpack jdk1.8_1.8.0221-1_amd64.deb ...
Unpacking jdk1.8 (1.8.0221-1) ...
Setting up jdk1.8 (1.8.0221-1) ...
coder@codercom-code-server1:~/temp$
5、安装完成后配置环境变量:
使用以下命令查到java安装路径
coder@codercom-code-server1:/usr/share$ whereis java
java: /usr/bin/java /usr/share/java /usr/java/jdk1.8.0_221-amd64/bin/java
coder@codercom-code-server1:/usr/java/jdk1.8.0_221-amd64$ sudo vi /etc/profile
在vi中执行以下命令:
:$ ##跳转到文本的最后一行
o ##直接插入行
然后将下面的jdk路径替换后,直接使用CTRL+ins键粘贴:
#set oracle jdk environment
export JAVA_HOME=/usr/java/jdk1.8.0_221-amd64 ##注意jdk目录要换成自己实际的jdk目录
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
然后:
:wq ##保存退出
加载新添加的配置:
coder@codercom-code-server1:/usr/java/jdk1.8.0_221-amd64$ . /etc/profile
6、但是使用java -version查询报错:
coder@codercom-code-server1:/usr/java/jdk1.8.0_221-amd64$ java -version
Error occurred during initialization of VM
java/lang/NoClassDefFoundError: java/lang/Object
查资料得知该报错是因为tools和rt解压失败导致。
切换到java安装路径,并使用jdk自带的unpack200进行解压:
cd /usr/java/jdk1.8.0_221-amd64/lib
sudo ../bin/unpack200 tools.pack tools.jar
cd /usr/java/jdk1.8.0_221-amd64/jre/lib
sudo ../../bin/unpack200 rt.pack rt.jar
然后再使用java -version查询就正常了: