Ubuntu 下配置 Java JDK 应该是每个Java开发者必备的技能,之前总是草草配置没有总结和回顾,今天总结一下!
本文源自部分内容源于网络,配置过程整理备忘使用,本文最经典之处是多版本JDK之间切换方法!
安装环节
1、卸载系统自带OpenJdk
$ sudo apt-get purge openjdk*
2、安装下载完毕的JDK,目前Oracle官方提供的Linux 64位版本下载 JDK 1.7 有tar.gz 格式,但JDK 1.6有bin格式,本文以.bin方式记录。
$ chmod +x jdk-6u45-linux-x64.bin #添加执行权限
$ ./jdk-6u45-linux-x64.bin #运行后结果会将JDK解压缩到本目录
3、查看结果,目前本目录下有两个JDK版本,1.7是之前安装的
$ ll
drwxr-xr-x 8 john john 4096 Mar 27 2013 jdk1.6.0_45/
drwxr-xr-x 8 john john 4096 Apr 4 2013 jdk1.7.0_51/
4、创建JDK软链接并将其指向jdk1.7.0_51,需要那个就软链接那个!
$ ln -s jdk1.7.0_51 current
john@ubuntu:Jdk$ ll
lrwxrwxrwx 1 john john 11 Feb 9 18:29 current -> jdk1.7.0_51/
drwxr-xr-x 8 john john 4096 Mar 27 2013 jdk1.6.0_45/
drwxr-xr-x 8 john john 4096 Feb 9 18:27 jdk1.7.0_51/
配置环境变量
1、修改系统级别环境变量
$ sudo vim /etc/environment
2、添加如下内容,根据自身情况修改
JAVA_HOME="/home/john/Developer/Jdk/current"
CLASSPATH=".:$JAVA_HOME/lib"
3、修改完毕,使环境变量立即生效
$ source /etc/environment
设置默认Java解析器
1、查看当前默认Java解析器
$ update-alternatives --display java
java - auto mode
link currently points to /usr/bin/gij-4.6
/usr/bin/gij-4.6 - priority 1046
slave java.1.gz: /usr/share/man/man1/gij-4.6.1.gz
Current 'best' version is '/usr/bin/gij-4.6'.
2、添加Java解析器
$ sudo update-alternatives --install /usr/bin/java java ~/Developer/Jdk/current/bin/java 300
3、配置默认Java解析器,注意权限,这里本机选择项目1进行配置
$ sudo update-alternatives --config java4、打完收工,查看配置选项是否正确!
There are 2 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/gij-4.6 1046 auto mode
1 /home/john/Developer/Jdk/current/bin/java 300 manual mode
2 /usr/bin/gij-4.6 1046 manual mode
Press enter to keep the current choice[*], or type selection number: 1
update-alternatives: using /home/john/Developer/Jdk/current/bin/java to provide /usr/bin/java (java) in manual mode.
alternative 模式已经由自动变为手动!
$ update-alternatives --display java
java - manual mode
link currently points to /home/john/Developer/Jdk/current/bin/java
/home/john/Developer/Jdk/current/bin/java - priority 300
/usr/bin/gij-4.6 - priority 1046
slave java.1.gz: /usr/share/man/man1/gij-4.6.1.gz
Current 'best' version is '/usr/bin/gij-4.6'.
查看Java版本
$ java -version
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)
感谢以下作者,排名不分先后 :)
Ubuntu 12.04 卸载自带OpenJDK, 手动下载IDK并配置Java运行环境