版本配置Linux系统的默认Java版本(linux默认java)
Linux系统中默认使用OpenJDK作为安装的JRE和JDK,不同发行版本安装的默认Java版本不同,可以在同一台机器上安装多个版本的Java,并定义其默认版本。本文将介绍如何配置Linux系统的默认Java版本。
1. 首先,我们要确定本机上安装的可用Java版本。可以使用以下命令查看:
$ java -version
java version “1.8.0_181”Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
2. 接下来,我们需要找到这些可用版本对应的JRE和JDK文件所在的位置,可以使用以下命令查找:
$ sudo find / -name “java”
/usr/lib/jvm/java-7-openjdk-amd64/usr/lib/jvm/java-8-openjdk-amd64
3. 接下来,可以使用update-alternatives命令来配置默认Java版本。比如可以使用以下命令来配置Java 8为默认版本:
$ sudo update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).Selection path Priority Status
------------------------------------------------------------* 0 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 auto mode
1 /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java 1071 manual mode2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode
# 选择需要配置的Java版本$ sudo update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).Selection Path Priority Status
------------------------------------------------------------ 0 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 auto mode
* 1 /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java 1071 manual mode 2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode
Press to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java to provide /usr/bin/java (java) in manual mode
4. 接着,我们可以使用以下命令来配置JDK:
$ sudo update-alternatives --config javac
There are 2 choices for the alternative javac (providing /usr/bin/javac).Selection Path Priority Status
------------------------------------------------------------* 0 /usr/lib/jvm/java-8-openjdk-amd64/bin/javac 1081 auto mode
1 /usr/lib/jvm/java-7-openjdk-amd64/bin/javac 1071 manual mode2 /usr/lib/jvm/java-8-openjdk-amd64/bin/javac 1081 manual mode
# 选择需要配置的Java版本$ sudo update-alternatives --config javac
There are 2 choices for the alternative javac (providing /usr/bin/javac).Selection Path Prority Status
------------------------------------------------------------ 0 /usr/lib/jvm/java-8-openjdk-amd64/bin/javac 1081 auto mode
* 1 /usr/lib/jvm/java-7-openjdk-amd64/bin/javac 1071 manual mode 2 /usr/lib/jvm/java-8-openjdk-amd64/bin/jAvac 1081 manual mode
Press to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/lib/jvm/java-8-openjdk-amd64/bin/javac to provide /usr/bin/javac (javac) in manual mode
通过以上步骤,我们就可以配置Linux系统的默认Java版本了。可以查看当前的版本配置:
$ java -version
java version “1.8.0_181”Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
总结一下,通过update-alternatives命令可以方便的配置Linux系统的默认Java版本,从而使应用能够使用指定版本的Java程序。