Ubuntu下切換多個JDK環境

安裝Ubuntu時,默認安裝了OpenJDK_1.7html

$ java -versionjava

java version "1.7.0_79"linux

OpenJDK Runtime Environment (IcedTea 2.5.6) (7u79-2.5.6-0ubuntu1.12.04.1)ubuntu

OpenJDK 64-Bit Server VM (build 24.79-b02, mixed mode)bash


如今須要安裝JDK_1.6oracle

安裝在/opt/java/jdk1.6.0_45app

/opt/java/jdk1.6.0_45# lsjvm

bin        db       jre  LICENSE  README.html  THIRDPARTYLICENSEREADME.txtide

COPYRIGHT  include  lib  man      src.zipui


加上環境變量

用戶環境變量:~/.bashrc中增長行

export JAVA_HOME=/opt/java/jdk1.6.0_45

export JRE_HOME=$JAVA_HOME/jre

export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JRE_HOME/lib/tools.jar

export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin

export USE_CCACHE=1


系統環境變量:/etc/profile中增長行

export JAVA_HOME=/opt/java/jdk1.6.0_45

export JRE_HOME=$JAVA_HOME/jre

export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JRE_HOME/lib/tools.jar

export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin

export USE_CCACHE=1


經過update-alternatives切換軟件版本

增長JDK_1.6的命令鏈接符,使用--install選項

$ sudo update-alternatives --install /usr/bin/java java /opt/java/jdk1.6.0_45/bin/java 1062  

如需移除,--remove選項

$ sudo update-alternatives --remove /usr/bin/java java /opt/java/jdk1.6.0_45/bin/java 1062

建立默認命令連接 --config

$ sudo update-alternatives --config java

There are 2 choices for the alternative java (providing /usr/bin/java).


  Selection    Path                   Priority  Status

------------------------------------------------------------------------------------

* 0    /opt/java/jdk1.6.0_45/bin/java            1062      auto mode

 1    /opt/java/jdk1.6.0_45/bin/java            1062      manual mode

 2    /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1051      manual mode


Press enter to keep the current choice[*], or type selection number:0


同理Python也能夠這樣切換


2017-6-16新增

更爲詳細的英文引用mark以下:

Use Oracle Java JDK, by following the steps below:

Here is how to configure Oracle Java JDK to be the default on Ubuntu:

cd /usr/lib/jvm && sudo /bin/sh ~/Downloads/jdk-6u38-linux-x64.bin -noregister

Note: You might get /bin/sh: jdk-6u43-linux-x64.bin: Permission denied error. This happens because root does not permissions to read the ~/Downloads folder.

Option 1: Temporarily change the permissions of ~/Downloads folder for root to have read permission.

chmod 755 ~/Downloads/

Don’t forget to change it back once you are done!

Option 2: Copy the downloaded file to /usr/lib/jvm and do

sudo /bin/sh jdk-6u38-linux-x64.bin -noregister

In either case, then set it as default.

sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.6.0_38/bin/javac 50000
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.6.0_38/bin/java 50000
sudo update-alternatives --install /usr/bin/javaws javaws /usr/lib/jvm/jdk1.6.0_38/bin/javaws 50000
sudo update-alternatives --install /usr/bin/javap javap /usr/lib/jvm/jdk1.6.0_38/bin/javap 50000
sudo update-alternatives --install /usr/bin/jar jar /usr/lib/jvm/jdk1.6.0_38/bin/jar 50000
sudo update-alternatives --install /usr/bin/jarsigner jarsigner /usr/lib/jvm/jdk1.6.0_38/bin/jarsigner 50000
sudo update-alternatives --config javac
sudo update-alternatives --config java
sudo update-alternatives --config javaws
sudo update-alternatives --config javap
sudo update-alternatives --config jar
sudo update-alternatives --config jarsigner

Also make sure /usr/bin/java is first in your $PATH. i.e.

which java

should return /usr/bin/java.

相關文章
相關標籤/搜索