(1).安装或升级java,并配置环境变量
注意:此处分为yum安装与rpm安装,区别在于yum安装省事但配置麻烦,rpm安装麻烦但配置省事。
1)yum安装
在安装前可以使用yum list installed | grep jdk查看已经安装的JDK文件
[root@xuexi ~]# yum list installed | grep jdk copy-jdk-configs.noarch 3.3-10.el7_5 @updates java-1.8.0-openjdk.x86_64 1:1.8.0.212.b04-0.el7_6 @updates java-1.8.0-openjdk-headless.x86_64 1:1.8.0.212.b04-0.el7_6 @updates
再使用yum list | grep java-1.8查看1.8版本的实际安装包
[root@xuexi ~]$ yum list | grep java-1.8.0 java-1.8.0-openjdk.x86_64 1:1.8.0.212.b04-0.el7_6 @updates java-1.8.0-openjdk-headless.x86_64 1:1.8.0.212.b04-0.el7_6 @updates java-1.8.0-openjdk.i686 1:1.8.0.212.b04-0.el7_6 updates java-1.8.0-openjdk-accessibility.i686 1:1.8.0.212.b04-0.el7_6 updates java-1.8.0-openjdk-accessibility.x86_64 java-1.8.0-openjdk-accessibility-debug.i686 java-1.8.0-openjdk-accessibility-debug.x86_64 java-1.8.0-openjdk-debug.i686 1:1.8.0.212.b04-0.el7_6 updates java-1.8.0-openjdk-debug.x86_64 1:1.8.0.212.b04-0.el7_6 updates java-1.8.0-openjdk-demo.i686 1:1.8.0.212.b04-0.el7_6 updates java-1.8.0-openjdk-demo.x86_64 1:1.8.0.212.b04-0.el7_6 updates java-1.8.0-openjdk-demo-debug.i686 1:1.8.0.212.b04-0.el7_6 updates java-1.8.0-openjdk-demo-debug.x86_64 1:1.8.0.212.b04-0.el7_6 updates java-1.8.0-openjdk-devel.i686 1:1.8.0.212.b04-0.el7_6 updates java-1.8.0-openjdk-devel.x86_64 1:1.8.0.212.b04-0.el7_6 updates java-1.8.0-openjdk-devel-debug.i686 1:1.8.0.212.b04-0.el7_6 updates java-1.8.0-openjdk-devel-debug.x86_64 1:1.8.0.212.b04-0.el7_6 updates java-1.8.0-openjdk-headless.i686 1:1.8.0.212.b04-0.el7_6 updates java-1.8.0-openjdk-headless-debug.i686 1:1.8.0.212.b04-0.el7_6 updates java-1.8.0-openjdk-headless-debug.x86_64 java-1.8.0-openjdk-javadoc.noarch 1:1.8.0.212.b04-0.el7_6 updates java-1.8.0-openjdk-javadoc-debug.noarch java-1.8.0-openjdk-javadoc-zip.noarch 1:1.8.0.212.b04-0.el7_6 updates java-1.8.0-openjdk-javadoc-zip-debug.noarch java-1.8.0-openjdk-src.i686 1:1.8.0.212.b04-0.el7_6 updates java-1.8.0-openjdk-src.x86_64 1:1.8.0.212.b04-0.el7_6 updates java-1.8.0-openjdk-src-debug.i686 1:1.8.0.212.b04-0.el7_6 updates java-1.8.0-openjdk-src-debug.x86_64 1:1.8.0.212.b04-0.el7_6 updates
差太多了,我这里使用yum -y install java-1.8*安装下。
在使用yum安装时是没有办法指定文件存放在哪里的,所以jdk文件也不清楚在哪里,环境变量配置上就有一定的麻烦,这一点就没有rpm好。
我们使用which java看看java脚本最终指向何处
[root@xuexi ~]$ which java /usr/bin/java [root@xuexi ~]$ ll /usr/bin/java lrwxrwxrwx. 1 root root 22 5月 5 16:17 /usr/bin/java -> /etc/alternatives/java [root@xuexi ~]$ ll /etc/alternatives/java lrwxrwxrwx. 1 root root 73 5月 5 16:17 /etc/alternatives/java -> /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.212.b04-0.el7_6.x86_64/jre/bin/java [root@xuexi ~]$ ll /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.212.b04-0.el7_6.x86_64/jre/bin/java -rwxr-xr-x. 1 root root 7376 4月 18 01:18 /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.212.b04-0.el7_6.x86_64/jre/bin/java
这里和Windows就很类似了,我们看看有没有必要的文件
[root@xuexi ~]# ls /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.212.b04-0.el7_6.x86_64 bin demo include jre lib sample src.zip tapset [root@xuexi ~]# ls /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.212.b04-0.el7_6.x86_64/lib amd64 dt.jar jconsole.jar orb.idl tools.jar ct.sym ir.idl jexec sa-jdi.jar [root@xuexi ~]# ls /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.212.b04-0.el7_6.x86_64/bin appletviewer java jconsole jps native2ascii schemagen xjc clhsdb javac jdb jrunscript orbd serialver extcheck javadoc jdeps jsadebugd pack200 servertool hsdb javah jhat jstack policytool tnameserv idlj javap jinfo jstat rmic unpack200 jar java-rmi.cgi jjs jstatd rmid wsgen jarsigner jcmd jmap keytool rmiregistry wsimport
没有问题,接下来就可以像Windows一样开始配置环境变量。注意有四个配置文件加载顺序是/etc/profile>>/etc/bashrc>>~/.bashrc>>~/.bash_profile,请根据情况进行文件修改。详见:
Shell变量的(4).添加环境变量
此处以/etc/profile配置文件为例,在文件末尾添加如下行:
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.212.b04-0.el7_6.x86_64 export PATH=${JAVA_HOME}/bin:$PATH export CLASSPATH=.:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar
接着刷新下配置文件
[root@xuexi ~]# source /etc/profile
最后测试一下
[root@xuexi ~]# java -version openjdk version "1.8.0_212" OpenJDK Runtime Environment (build 1.8.0_212-b04) OpenJDK 64-Bit Server VM (build 25.212-b04, mixed mode) [root@xuexi ~]# echo $JAVA_HOME /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.212.b04-0.el7_6.x86_64
2)rpm安装
首先需要下载一份rpm安装包并上传至CentOS7系统,使用rpm -ivh jdk-8u212-linux-x64.rpm安装或升级java到1.8版本。
[root@xuexi ~]# rpm -ivh jdk-8u212-linux-x64.rpm 警告:jdk-8u212-linux-x64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID ec551f03: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:jdk1.8-2000:1.8.0_212-fcs ################################# [100%] Unpacking JAR files... tools.jar... plugin.jar... javaws.jar... deploy.jar... rt.jar... jsse.jar... charsets.jar... localedata.jar...
接着可以使用rpm -pql /root/jdk-8u212-linux-x64.rpm看到安装包安装的目录(会列出所有,请及时停止)。当然也可以使用rpm -ivh --prefix=<dir> jdk-8u212-linux-x64.rpm安装到指定目录下。
看一下安装目录结构以便配置环境变量
[root@xuexi ~]# ls /usr/java/ default jdk1.8.0_212-amd64 latest [root@xuexi ~]# ls /usr/java/jdk1.8.0_212-amd64/ bin lib src.zip COPYRIGHT LICENSE THIRDPARTYLICENSEREADME-JAVAFX.txt include man THIRDPARTYLICENSEREADME.txt javafx-src.zip README.html jre release
修改/etc/profile文件,在末尾添加如下三行
export JAVA_HOME=/usr/java/jdk1.8.0_212-amd64 export PATH=${JAVA_HOME}/bin:$PATH export CLASSPATH=.:${JAVA_HOME}/lib/dt.jzr:${JAVA_HOME}/lib/tools.jar
然后刷新配置文件
[root@xuexi ~]# source /etc/profile
最后测试一下
[root@xuexi ~]# echo $JAVA_HOME /usr/java/jdk1.8.0_212-amd64 您在 /var/spool/mail/root 中有新邮件 [root@xuexi ~]# java -version java version "1.8.0_212" Java(TM) SE Runtime Environment (build 1.8.0_212-b10) Java HotSpot(TM) 64-Bit Server VM (build 25.212-b10, mixed mode)