Ubuntu下切换多个JDK环境-创新互联-成都快上网建站

Ubuntu下切换多个JDK环境-创新互联

安装Ubuntu时,默认安装了OpenJDK_1.7

目前创新互联公司已为数千家的企业提供了网站建设、域名、网络空间、网站托管、企业网站设计、商河网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

$ java -version

java version "1.7.0_79"

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

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

现在需要安装JDK_1.6

安装在/opt/java/jdk1.6.0_45

/opt/java/jdk1.6.0_45# ls

bin     db    jre  LICENSE  README.html  THIRDPARTYLICENSEREADME.txt

COPYRIGHT  include  lib  man    src.zip

加上环境变量

用户环境变量:~/.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:

  • Visit http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javase6-419409.html

  • Accept the license agreement

  • Download jdk-6uXX-linux-x64.bin (where XX is the latest minor version)

  • If you don't have an Oracle account and don't want to create one, you can have a look at http://bugmenot.com/view/oracle.com

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.

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站题目:Ubuntu下切换多个JDK环境-创新互联
网页路径:http://kswjz.com/article/cscege.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流