java多版本管理


java多版本管理

适用于ubuntu

检查可用的apt中的openjdk源

$ apt --names-only search "openjdk-.*jre$"
Sorting... Done
Full Text Search... Done
openjdk-11-jre/focal-updates,focal-security,focal-updates,focal-security,focal-security,focal-updates 11.0.22+7-0ubuntu2~20.04.1 amd64
  OpenJDK Java runtime, using Hotspot JIT

openjdk-13-jre/focal-updates,focal-updates,focal-updates 13.0.7+5-0ubuntu1~20.04 amd64
  OpenJDK Java runtime, using Hotspot JIT

openjdk-16-jre/focal-updates,focal-security,focal-updates,focal-security,focal-security,focal-updates 16.0.1+9-1~20.04 amd64
  OpenJDK Java runtime, using Hotspot JIT

openjdk-17-jre/focal-updates,focal-security,focal-updates,focal-security,focal-security,focal-updates 17.0.10+7-1~20.04.1 amd64
  OpenJDK Java runtime, using Hotspot JIT

openjdk-21-jre/focal-updates,focal-security,focal-updates,focal-security,focal-security,focal-updates,now 21.0.2+13-1~20.04.1 amd64 [installed]
  OpenJDK Java runtime, using Hotspot JIT

openjdk-8-jre/focal-security,focal-security,now 8u402-ga-2ubuntu1~20.04 amd64 [installed]
  OpenJDK Java runtime, using Hotspot JIT

安装jre

sudo apt install openjdk-21-jre
sudo apt install openjdk-8-jre

使用update-alternatives管理多个版本

  • 查看linux服务器是否拥有JDK的软链接

    update-alternatives --config java
  • 如果没有,那就使update-alternatives --install选项创建软链接

  • 修改java版本配置

    sudo update-alternatives --config java

    选择相应的版本即可

同理安装jdk

$ apt --names-only search "openjdk-.*jdk*$"
Sorting... Done
Full Text Search... Done
openjdk-11-jdk/focal-updates,focal-security,focal-updates,focal-security,focal-security,focal-updates 11.0.22+7-0ubuntu2~20.04.1 amd64
  OpenJDK Development Kit (JDK)

openjdk-13-jdk/focal-updates,focal-updates,focal-updates 13.0.7+5-0ubuntu1~20.04 amd64
  OpenJDK Development Kit (JDK)

openjdk-16-jdk/focal-updates,focal-security,focal-updates,focal-security,focal-security,focal-updates 16.0.1+9-1~20.04 amd64
  OpenJDK Development Kit (JDK)

openjdk-17-jdk/focal-updates,focal-security,focal-updates,focal-security,focal-security,focal-updates 17.0.10+7-1~20.04.1 amd64
  OpenJDK Development Kit (JDK)

openjdk-21-jdk/focal-updates,focal-security,focal-updates,focal-security,focal-security,focal-updates,now 21.0.2+13-1~20.04.1 amd64 [installed]
  OpenJDK Development Kit (JDK)

openjdk-8-jdk/focal-security,focal-security 8u402-ga-2ubuntu1~20.04 amd64
  OpenJDK Development Kit (JDK)

安装jdk

sudo apt install openjdk-21-jdk
sudo apt install openjdk-8-jdk

使用update-alternatives管理多个版本,记得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-21-openjdk-amd64/bin/javac   2111      auto mode
  1            /usr/lib/jvm/java-21-openjdk-amd64/bin/javac   2111      manual mode
* 2            /usr/lib/jvm/java-8-openjdk-amd64/bin/javac    1081      manual mode

设置JAVA_HOME等环境变量

export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

这个在用到的时候还是得自己手动修改

安装oracle家的java,先本地下载再scp上去

$ ls *jdk*
jdk-8u101-linux-x64.tar.gz
$ tar -zxvf jdk-8u101-linux-x64.tar.gz

设置环境变量

vim ~/.profile

这里在自己根目录下面设置,可以避免影响系统环境

在最后面添加:

JAVA_HOME=/home/hj/jdk1.8.0_101
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME
export PATH
export CLASSPATH

然后source ~/.profile即可。当然写到.bashrc也行,不同功能罢了


文章作者: hugo
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 hugo !
  目录