1、下载Oracle Java JDK 12
从下面的链接下载最新的Java SE开发工具包12 LTS版本:
http://www.oracle.com/technetwork/java/javase/downloads/index.html
通过运行以下命令安装Java DEB包:
cd /tmp
wget --no-cookies --no-check-certificate --header "Cookie: oraclelicense=accept-securebackup-cookie" https://download.oracle.com/otn-pub/java/jdk/12+33/312335d836a34c7c8bba9d963e26dc23/jdk-12_linux-x64_bin.deb
2、安装Oracle Java JDK 12
1)在deb包所在目录,运行如下命令安装Oracle Java JDK:
sudo dpkg -i jdk-12_linux-x64_bin.deb
2)使用update-alternatives配置Oracle Java JDK 12为Ubuntu配置JDK:
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-12/bin/java 2
sudo update-alternatives --config java
如果您安装了其他版本的Java,并且运行了上面的命令,您应该会看到一个提示,选择您想要设置默认值的Java版本
接下来,使Java 12成为Ubuntu默认Java编译器,运行下面的命令:
sudo update-alternatives --install /usr/bin/jar jar /usr/lib/jvm/jdk-12/bin/jar 2
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-12/bin/javac 2
sudo update-alternatives --set jar /usr/lib/jvm/jdk-12/bin/jar
sudo update-alternatives --set javac /usr/lib/jvm/jdk-12/bin/javac
运行下面命令验证是否安装成功:
java -version
成功则输出下面提示信息:
java version "12" 2019-03-19
Java(TM) SE Runtime Environment (build 12+33)
Java HotSpot(TM) 64-Bit Server VM (build 12+33, mixed mode, sharing)
3、创建Java环境变量
要设置JAVA环境变量,请在/etc//etc/profile.d
目录中创建一个新文件,使用如下命令:
sudo nano /etc/profile.d/jdk12.sh
jdk12.sh文件内容如下:
export J2SDKDIR=/usr/lib/jvm/java-12
export J2REDIR=/usr/lib/jvm/java-12
export PATH=$PATH:/usr/lib/jvm/java-12/bin:/usr/lib/jvm/java-12/db/bin
export JAVA_HOME=/usr/lib/jvm/java-12
export DERBY_HOME=/usr/lib/jvm/java-12/db
使配置文件生效,运行如下命令:
source /etc/profile.d/jdk12.sh
运行下面命令验证是否安装成功:
java -version
成功则输出下面提示信息:
java version "12" 2019-03-19
Java(TM) SE Runtime Environment (build 12+33)
Java HotSpot(TM) 64-Bit Server VM (build 12+33, mixed mode, sharing)