tar 包安装方式
## 下载地址:
## oracle jdk: http://www.oracle.com/technetwork/java/javase/downloads/index.html
## open jdk: http://jdk.java.net/10/
## oracle jdk 10
cd /opt
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/10.0.1+10/fb4372174a714e6b8c52526dc134031e/jdk-10.0.1_linux-x64_bin.tar.gz"
## oracle jdk 8
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u171-b11/512cd62ec5174c3487ac17c61aaa89e8/jdk-8u171-linux-x64.tar.gz"
## openjdk 10
wget https://download.java.net/java/GA/jdk10/10.0.1/fb4372174a714e6b8c52526dc134031e/10/openjdk-10.0.1_linux-x64_bin.tar.gz
## 解压
tar -zxf jdk-10.0.1_linux-x64_bin.tar.gz -C /usr/local
ls /usr/local/jdk-10.0.1/
## 添加JAVA JDK环境变量:
1 | cat > /etc/profile.d/jdk.sh << EOF |
cat /etc/profile.d/jdk.sh
JAVA_HOME=/usr/local/jdk-10.0.1
JRE_HOME=/usr/local/jdk-10.0.1/jre
CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export JAVA_HOME JRE_HOME PATH CLASSPATH
## 手动加载java环境变量
chmod 644 /etc/profile.d/jdk.sh
source /etc/profile.d/jdk.sh
## 验证
# java -version
java 10.0.1 2018-04-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)
yum 本地安装方式
## 如果rpm 或 yum 安装过jdk, 清理
## 查询
rpm -qa | grep -E '^open[jre|jdk]|j[re|dk]'
....
jdk1.8.0_131-1.8.0_131-fcs.x86_64
....
## 移除
yum -y remove jdk1.8.0_131-1.8.0_131-fcs.x86_64
##==========================================##
## rpm 方式安装jdk 10
##==========================================##
## orcle jdk 10
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/10.0.1+10/fb4372174a714e6b8c52526dc134031e/jdk-10.0.1_linux-x64_bin.rpm"
## orcle jdk 8
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u171-b11/512cd62ec5174c3487ac17c61aaa89e8/jdk-8u171-linux-x64.rpm"
## yum 本地安装方便以后反安装
yum localinstall jdk-10.0.1_linux-x64_bin.rpm
## rpm 安装也可以
rpm -ivh jdk-10.0.1_linux-x64_bin.rpm
## 安装目录:/usr/java/jdk-10.0.1
多版本切换
usage: alternatives --install <link> <name> <path> <priority>
[--initscript <service>]
[--family <family>]
[--slave <link> <name> <path>]*
alternatives --remove <name> <path>
alternatives --auto <name> # 将手动设置为自动选择最高优先级的,如:alternatives --auto java
alternatives --config <name> # 手动设置版本
alternatives --display <name> # 选项的功能就是查看一个命令链接组的所有信息,包括链接的模式(自动还是手动)、链接priority值、所有可用的链接命令等等
alternatives --set <name> <path> # 直接设置路径,像config,但不是通过交互式选择
alternatives --list # 列出所有命令当前设置
说明:
alternatives --install <link> <name> <path> <priority>
其中,
install: 表示安装,就是增加一组新的系统命令链接符了
link: 是符号链接,为系统中功能相同软件的公共链接目录
name: 为命令链接符名称,比如java
path: 为你所要使用新命令、新软件的所在目录
priority: 则表示优先级,数值标识
slave: 为从alternative
## 比如通过tar包安装jdk8,路径 /usr/local/jdk-8/, 优先级为2,最后调整为系统默认的版本
cd /usr/local/jdk-8/
alternatives --install /usr/bin/java java /usr/local/jdk-8/bin/java 2
alternatives --install /usr/bin/javac javac /usr/local/jdk-8/bin/javac 2
alternatives --install /usr/bin/jar jar /usr/local/jdk-8/bin/jar 2
alternatives --set jar /usr/local/jdk-8/bin/jar
alternatives --set javac /usr/local/jdk-8/bin/javac
## config 选项功能为在现有的命令链接选择一个作为系统默认的,使用语法为:alternatives --config name
alternatives --config java
There are 2 programs which provide 'java'.
Selection Command
-----------------------------------------------
*+ 1 /usr/java/jdk-10.0.1/bin/java
2 /usr/java/jdk1.8.0_171-amd64/jre/bin/java
## 删除
alternatives --remove java java /usr/local/jdk-8/bin/java
rpm 包安装方式
1. 到oracle官网下载RPM 包
## http://www.oracle.com/technetwork/java/javase/downloads/index.html
## --------------- For 64-bit Systems ---------------
cd /opt
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u45-b14/jdk-8u45-linux-x64.rpm"
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u45-b14/jre-8u45-linux-x64.rpm"
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.rpm"
## http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.rpm
2. 安装
## --------------- For 64-bit Systems ---------------
rpm -Uvh jdk-8u45-linux-x64.rpm
rpm -Uvh jre-8u45-linux-x64.rpm
3. 检查
java -version
javac -version
4. Enable Java JDK / JRE 8u45 Support in Firefox
## For 32-Bit OS ##
alternatives --install /usr/lib/mozilla/plugins/libjavaplugin.so libjavaplugin.so /usr/java/jdk1.8.0_45/jre/lib/i386/libnpjp2.so 20000
## For 64-Bit OS ##
alternatives --install /usr/lib/mozilla/plugins/libjavaplugin.so libjavaplugin.so /usr/java/jdk1.8.0_45/jre/lib/amd64/libnpjp2.so 20000
安装open jdk
1) Installing Java JRE on CentOS 7, 下面选一个版本安装
sudo yum install java-1.8.0-openjdk
sudo yum install java-1.7.0-openjdk
sudo yum install java-1.6.0-openjdk
2) Installing Java JDK on CentOS 7
sudo yum install java-1.8.0-openjdk-devel
sudo yum install java-1.7.0-openjdk-devel
sudo yum install java-1.6.0-openjdk-devel
Installing Oracle Java JRE on CentOS 7
cd /opt
sudo wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" \
"http://download.oracle.com/otn-pub/java/jdk/8u51-b16/jre-8u51-linux-x64.tar.gz"
sudo tar xvf jre-8u51-linux-x64.tar.gz
sudo chown -R root: jre1.8.0_51
sudo alternatives --install /usr/bin/java java /opt/jre1.8.0_51/bin/java 1
We also recommend to setup javac and jar commands path using alternatives
# alternatives --install /usr/bin/jar jar /opt/jdk1.8.0_51/bin/jar 2
# alternatives --install /usr/bin/javac javac /opt/jdk1.8.0_51/bin/javac 2
# alternatives --set jar /opt/jdk1.8.0_51/bin/jar
# alternatives --set javac /opt/jdk1.8.0_51/bin/javac
alternatives command is available in chkconfig package.
Installing Oracle Java JDK on CentOS 7
cd /opt
sudo wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" \
"http://download.oracle.com/otn-pub/java/jdk/8u51-b16/jdk-8u51-linux-x64.tar.gz"
sudo tar xvf jdk-8u51-linux-x64.tar.gz
sudo chown -R root: jdk1.8.0_51
# 使用alternatives安装
sudo alternatives --install /usr/bin/java java /opt/jdk1.8.0_51/bin/java 1
Select your default Java Version on CentOS 7
# 如果系统安装了多个版本,查看各个版本的安装路径
sudo update-alternatives --config java
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.51-1.b16.el7_1.x86_64
/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.85-2.6.1.2.el7_1.x86_64
/usr/lib/jvm/jre-1.6.0-openjdk.x86_64
Setup JAVA_HOME on CentOS 7
#重新编辑配置文件
sudo vi /etc/profile
export JAVA_HOME="/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.51-1.b16.el7_1.x86_64"
#重新加载配置文件
source /etc/profile
# 查看运行状态下的变量值
echo $JAVA_HOME
修改 profile 文件
vi /etc/profile
在文件的末尾修改
export JAVA_HOME=/usr/java/jdk1.8.0_45
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
为
export JAVA_HOME=/usr/java/jdk1.8.0_45
export CATALINA_HOME=/usr/local/apache-tomcat-8.0.22
export PATH=$JAVA_HOME/bin:$PATH:$CATALINA_HOME/lib
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$CATALINA_HOME/bin
注意
JAVA_HOME 是你的 JDK 安装目录
Linux 下用冒号“:”来分隔路径
$PATH / $CLASSPATH / $JAVA_HOME 是用来引用原来的环境变量的值
export 是把这三个变量导出为全局变量。
方法2
上述修改 放到 .bash_profile 文件末尾中
这种方法更为安全,它可以把使用这些环境变量的权限控制到用户级别,如果你需要给某个用户权限使用这些环境变量,你只需要修改其个人用户主目录下的 .bash_profile文件就可以了。
参考:
https://www.digitalocean.com/community/tutorials/how-to-install-java-on-centos-and-fedora