CentOS安装JDK
1.卸载OpenJDK
2.下载JDK
3.安装JDK
一、下载JDK
从官网上下载对应系统版本的JDK,下载地址:
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
我们的系统是CentOS 7 ,这里可以选择rpm包
二、卸载OpenJDK、旧版本JDK
查看系统是否已安装JDK。一般的linux都默认使用了开源的openJDK。显示JDK版本信息,已经安装JDK,否则没有安装。命令行:
java -version
[root@localhost ~]# java -version
java version “1.7.0_79”
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)
查找名字包含java,jdk的已安装程序。查找到了,已经安装JDK,否则没有安装。命令行:
rpm -qa | grep java
rpm -qa | grep jdk
已安装JDK,卸载系统上的JDK。
单个卸载程序,使用rpm -e -nodeps xxx命令。命令行:
rpm -e -nodeps xxx
批量卸载所有名字包含jdk的已安装程序。命令行:
rpm -qa | grep jdk | xargs rpm -e --nodeps
[root@localhost]# rpm -qa | grep jdk | xargs rpm -e –nodeps
批量卸载所有名字包含java的已安装程序。命令行:
rpm -qa | grep java | xargs rpm -e --nodeps
卸载后,查看JDK版本,已无信息,卸载成功。命令行:
java -version
bash: /usr/bin/java: No such file or directory
卸载干净了,下一步安装JDK
三、安装配置JDK
1.rpm安装JDK
rpm -ivh jdk-8u131-linux-x64.rpm
默认安装在/usr/java/jdk1.8.0_131目录下
2.tar.gz后缀格式JDK安装方式
安装jdk-8u131-linux-x64.tar.gz。
在/usr目录下新建java文件夹,命令行:
mkdir /usr/java
进入JDK压缩包所在目录,将压缩包复制到java文件夹中。复制文件用cp xxx命令,复制文件夹用cp -r xxx,命令行:
cp jdk-8u131-linux-x64.tar.gz /usr/java
然后返回到根目录,再进入java目录,解压文件并删除压缩包,命令行:
cd /usr/java
tar -zxvf jdk-8u131-linux-x64.tar.gz
rm jdk-8u131-linux-x64.tar.gz
3.修改环境变量
修改/etc/profile文件设置系统变量,设置jdk环境变量,该方式对所有用户有效。
使用VI编辑,输入命令,回车确认。命令行:
vi /etc/profile
打开之后在文件末尾添加下面配置。
export JAVA_HOME=/usr/java/jdk1.8.0_131
export JRE_HOME=$JAVA_HOME/jre
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=./:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
配置好后,按Esc退出,按Shift加英文冒号shift + : 然后输入wq,按Enter回车键确认。取消编辑按Ctrl+z。
使profile配置生效。命令行:
source /etc/profile
4.验证
[root@localhost ~]# java -version
java version “1.8.0_131”
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
[root@localhost ~]# javac
Usage: javac
where possible options include:
-g Generate all debugging info
-g:none Generate no debugging info
-g:{lines,vars,source} Generate only some debugging info
-nowarn Generate no warnings
-verbose Output messages about what the compiler is doing
-deprecation Output source locations where deprecated APIs are used
-classpath Specify where to find user class files and annotation processors
-cp Specify where to find user class files and annotation processors
-sourcepath Specify where to find input source files
-bootclasspath Override location of bootstrap class files
-extdirs Override location of installed extensions
-endorseddirs Override location of endorsed standards path
-proc:{none,only} Control whether annotation processing and/or compilation is done.
-processor [,,…] Names of the annotation processors to run; bypasses default discovery process
-processorpath Specify where to find annotation processors
-d Specify where to place generated class files
-s Specify where to place generated source files
-implicit:{none,class} Specify whether or not to generate class files for implicitly referenced files
-encoding Specify character encoding used by source files
-source Provide source compatibility with specified release
-target Generate class files for specific VM version
-version Version information
-help Print a synopsis of standard options
-Akey[=value] Options to pass to annotation processors
-X Print a synopsis of nonstandard options
-J Pass directly to the runtime system
-Werror Terminate compilation if warnings occur
@ Read options and filenames from file