1、通过apt-get直接下载安装JDK
sudo apt-get install openjdk-7-jdk
执行之后会将JDK安装到/usr/lib/jvm/java-7-openjdk-i386/
2、通过update-alternatives选择系统中使用的java
sudo update-alternatives --config java 有 3 个候选项可用于替换 java (提供 /usr/bin/java)。 选择 路径 优先级 状态 ------------------------------------------------------------ * 0 /usr/lib/jvm/java-6-openjdk-i386/jre/bin/java 1061 自动模式 /usr/lib/jvm/java-6-openjdk-i386/jre/bin/java 1061 手动模式 /usr/lib/jvm/java-6-sun/jre/bin/java 63 手动模式 /usr/lib/jvm/java-7-openjdk-i386/jre/bin/java 1051 手动模式 要维持当前值[*]请按回车键,或者键入选择的编号:3 update-alternatives: 使用 /usr/lib/jvm/java-7-openjdk-i386/jre/bin/java 来提供 /usr/bin/java (java),于 手动模式 中。
实际是将/usr/bin/java链接到JDK7上,保证执行java命令后,实际运行的是我们的目标版本
java -version java version "1.7.0_09" OpenJDK Runtime Environment (IcedTea7 2.3.3) (7u9-2.3.3-0ubuntu1~12.04.1) OpenJDK Server VM (build 23.2-b09, mixed mode)
除了java外,还有javac、jar、javah、javap、jps、jconsole等
sudo update-alternatives --config javac sudo update-alternatives --config jar sudo update-alternatives --config javah sudo update-alternatives --config javap sudo update-alternatives --config jps sudo update-alternatives --config jconsole
补充:jps -lv 用于查看java程序的启动参数,如java版本
二、全手动
1、访问http://www.oracle.com/technetwork/java/javase/downloads/index.html,下载jdk7
oracle没有提供deb的安装包,只能下载压缩包jdk-7u10-linux-i586.tar.gz
2、下载完成后,命令解压到/usr/lib/jvm
sudo tar zxvf ./jdk-7u10-linux-i586.tar.gz -C /usr/lib/jvm
3、为JDK目录创建软连接到java-7-sun
sudo ln -s jdk1.7.0_10 java-7-sun
4、将javac、jar、javah、javap等添加到候选列表
sudo update-alternatives --install /usr/bin/ControlPanel ControlPanel /usr/lib/jvm/java-7-sun/jre/bin/ControlPanel 300 sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-7-sun/jre/bin/java 300 sudo update-alternatives --install /usr/bin/java_vm java_vm /usr/lib/jvm/java-7-sun/jre/bin/java_vm 300 sudo update-alternatives --install /usr/bin/javaws javaws /usr/lib/jvm/java-7-sun/jre/bin/javaws 300 sudo update-alternatives --install /usr/bin/jcontrol jcontrol /usr/lib/jvm/java-7-sun/jre/bin/jcontrol 300 sudo update-alternatives --install /usr/bin/keytool keytool /usr/lib/jvm/java-7-sun/jre/bin/keytool 300 sudo update-alternatives --install /usr/bin/pack200 pack200 /usr/lib/jvm/java-7-sun/jre/bin/pack200 300 sudo update-alternatives --install /usr/bin/policytool policytool /usr/lib/jvm/java-7-sun/jre/bin/policytool 300 sudo update-alternatives --install /usr/bin/rmid rmid /usr/lib/jvm/java-7-sun/jre/bin/rmid 300 sudo update-alternatives --install /usr/bin/rmiregistry rmiregistry /usr/lib/jvm/java-7-sun/jre/bin/rmiregistry 300 sudo update-alternatives --install /usr/bin/unpack200 unpack200 /usr/lib/jvm/java-7-sun/jre/bin/unpack200 300 sudo update-alternatives --install /usr/bin/orbd orbd /usr/lib/jvm/java-7-sun/jre/bin/orbd 300 sudo update-alternatives --install /usr/bin/servertool servertool /usr/lib/jvm/java-7-sun/jre/bin/servertool 300 sudo update-alternatives --install /usr/bin/tnameserv tnameserv /usr/lib/jvm/java-7-sun/jre/bin/tnameserv 300 sudo update-alternatives --install /usr/bin/HtmlConverter HtmlConverter /usr/lib/jvm/java-7-sun/bin/HtmlConverter 300 sudo update-alternatives --install /usr/bin/appletviewer appletviewer /usr/lib/jvm/java-7-sun/bin/appletviewer 300 sudo update-alternatives --install /usr/bin/apt apt /usr/lib/jvm/java-7-sun/bin/apt 300 sudo update-alternatives --install /usr/bin/extcheck extcheck /usr/lib/jvm/java-7-sun/bin/extcheck 300 sudo update-alternatives --install /usr/bin/idlj idlj /usr/lib/jvm/java-7-sun/bin/idlj 300 sudo update-alternatives --install /usr/bin/jar jar /usr/lib/jvm/java-7-sun/bin/jar 300 sudo update-alternatives --install /usr/bin/jarsigner jarsigner /usr/lib/jvm/java-7-sun/bin/jarsigner 300 sudo update-alternatives --install /usr/bin/java-rmi.cgi java-rmi.cgi /usr/lib/jvm/java-7-sun/bin/java-rmi.cgi 300 sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/java-7-sun/bin/javac 300 sudo update-alternatives --install /usr/bin/javadoc javadoc /usr/lib/jvm/java-7-sun/bin/javadoc 300 sudo update-alternatives --install /usr/bin/javah javah /usr/lib/jvm/java-7-sun/bin/javah 300 sudo update-alternatives --install /usr/bin/javap javap /usr/lib/jvm/java-7-sun/bin/javap 300 sudo update-alternatives --install /usr/bin/jconsole jconsole /usr/lib/jvm/java-7-sun/bin/jconsole 300 sudo update-alternatives --install /usr/bin/jdb jdb /usr/lib/jvm/java-7-sun/bin/jdb 300 sudo update-alternatives --install /usr/bin/jhat jhat /usr/lib/jvm/java-7-sun/bin/jhat 300 sudo update-alternatives --install /usr/bin/jinfo jinfo /usr/lib/jvm/java-7-sun/bin/jinfo 300 sudo update-alternatives --install /usr/bin/jmap jmap /usr/lib/jvm/java-7-sun/bin/jmap 300 sudo update-alternatives --install /usr/bin/jps jps /usr/lib/jvm/java-7-sun/bin/jps 300 sudo update-alternatives --install /usr/bin/jrunscript jrunscript /usr/lib/jvm/java-7-sun/bin/jrunscript 300 sudo update-alternatives --install /usr/bin/jsadebugd jsadebugd /usr/lib/jvm/java-7-sun/bin/jsadebugd 300 sudo update-alternatives --install /usr/bin/jstack jstack /usr/lib/jvm/java-7-sun/bin/jstack 300 sudo update-alternatives --install /usr/bin/jstat jstat /usr/lib/jvm/java-7-sun/bin/jstat 300 sudo update-alternatives --install /usr/bin/jstatd jstatd /usr/lib/jvm/java-7-sun/bin/jstatd 300 sudo update-alternatives --install /usr/bin/native2ascii native2ascii /usr/lib/jvm/java-7-sun/bin/native2ascii 300 sudo update-alternatives --install /usr/bin/rmic rmic /usr/lib/jvm/java-7-sun/bin/rmic 300 sudo update-alternatives --install /usr/bin/schemagen schemagen /usr/lib/jvm/java-7-sun/bin/schemagen 300 sudo update-alternatives --install /usr/bin/serialver serialver /usr/lib/jvm/java-7-sun/bin/serialver 300 sudo update-alternatives --install /usr/bin/wsgen wsgen /usr/lib/jvm/java-7-sun/bin/wsgen 300 sudo update-alternatives --install /usr/bin/wsimport wsimport /usr/lib/jvm/java-7-sun/bin/wsimport 300 sudo update-alternatives --install /usr/bin/xjc xjc /usr/lib/jvm/java-7-sun/bin/xjc 300
5、创建/usr/lib/jvm/.java-7-sun.jinfo ,输入以下的内容
name=java-7-sun-1.7.0_10 alias=java-7-sun priority=63 section=non-free jre ControlPanel /usr/lib/jvm/java-7-sun/jre/bin/ControlPanel jre java /usr/lib/jvm/java-7-sun/jre/bin/java jre java_vm /usr/lib/jvm/java-7-sun/jre/bin/java_vm jre javaws /usr/lib/jvm/java-7-sun/jre/bin/javaws jre jcontrol /usr/lib/jvm/java-7-sun/jre/bin/jcontrol jre keytool /usr/lib/jvm/java-7-sun/jre/bin/keytool jre pack200 /usr/lib/jvm/java-7-sun/jre/bin/pack200 jre policytool /usr/lib/jvm/java-7-sun/jre/bin/policytool jre rmid /usr/lib/jvm/java-7-sun/jre/bin/rmid jre rmiregistry /usr/lib/jvm/java-7-sun/jre/bin/rmiregistry jre unpack200 /usr/lib/jvm/java-7-sun/jre/bin/unpack200 jre orbd /usr/lib/jvm/java-7-sun/jre/bin/orbd jre servertool /usr/lib/jvm/java-7-sun/jre/bin/servertool jre tnameserv /usr/lib/jvm/java-7-sun/jre/bin/tnameserv jdk HtmlConverter /usr/lib/jvm/java-7-sun/bin/HtmlConverter jdk appletviewer /usr/lib/jvm/java-7-sun/bin/appletviewer jdk apt /usr/lib/jvm/java-7-sun/bin/apt jdk extcheck /usr/lib/jvm/java-7-sun/bin/extcheck jdk idlj /usr/lib/jvm/java-7-sun/bin/idlj jdk jar /usr/lib/jvm/java-7-sun/bin/jar jdk jarsigner /usr/lib/jvm/java-7-sun/bin/jarsigner jdk java-rmi.cgi /usr/lib/jvm/java-7-sun/bin/java-rmi.cgi jdk javac /usr/lib/jvm/java-7-sun/bin/javac jdk javadoc /usr/lib/jvm/java-7-sun/bin/javadoc jdk javah /usr/lib/jvm/java-7-sun/bin/javah jdk javap /usr/lib/jvm/java-7-sun/bin/javap jdk jconsole /usr/lib/jvm/java-7-sun/bin/jconsole jdk jdb /usr/lib/jvm/java-7-sun/bin/jdb jdk jhat /usr/lib/jvm/java-7-sun/bin/jhat jdk jinfo /usr/lib/jvm/java-7-sun/bin/jinfo jdk jmap /usr/lib/jvm/java-7-sun/bin/jmap jdk jps /usr/lib/jvm/java-7-sun/bin/jps jdk jrunscript /usr/lib/jvm/java-7-sun/bin/jrunscript jdk jsadebugd /usr/lib/jvm/java-7-sun/bin/jsadebugd jdk jstack /usr/lib/jvm/java-7-sun/bin/jstack jdk jstat /usr/lib/jvm/java-7-sun/bin/jstat jdk jstatd /usr/lib/jvm/java-7-sun/bin/jstatd jdk native2ascii /usr/lib/jvm/java-7-sun/bin/native2ascii jdk rmic /usr/lib/jvm/java-7-sun/bin/rmic jdk schemagen /usr/lib/jvm/java-7-sun/bin/schemagen jdk serialver /usr/lib/jvm/java-7-sun/bin/serialver jdk wsgen /usr/lib/jvm/java-7-sun/bin/wsgen jdk wsimport /usr/lib/jvm/java-7-sun/bin/wsimport jdk xjc /usr/lib/jvm/java-7-sun/bin/xjc plugin xulrunner-addons-javaplugin.so /usr/lib/jvm/java-7-sun/jre/plugin/i386/ns7/libjavaplugin_oji.so
说明,/usr/lib/jvm/.xxx.jinfo用于执行update-java-alternatives
6、执行update-java-alternatives
/usr/lib/jvm$ sudo update-java-alternatives -l java-1.6.0-openjdk-i386 1061 /usr/lib/jvm/java-1.6.0-openjdk-i386 java-1.7.0-openjdk-i386 1051 /usr/lib/jvm/java-1.7.0-openjdk-i386 java-6-sun 63 /usr/lib/jvm/java-6-sun java-7-sun 63 /usr/lib/jvm/java-7-sun /usr/lib/jvm$ sudo update-java-alternatives -s java-7-sun update-alternatives: 错误: 无 mozilla-javaplugin.so 的候选项。 update-alternatives: 错误: 无 xulrunner-addons-javaplugin.so 的候选项。 update-java-alternatives: jdk alternative does not exist: /usr/lib/jvm/java-7-sun/bin/HtmlConverter update-alternatives: 错误: wsgen 的候选项 /usr/lib/jvm/java-7-sun/bin/wsgen 没有注册,不予设置。 update-alternatives: 错误: 无 xulrunner-addons-javaplugin.so 的候选项。
如果需要配置浏览器,可以参照oracle的文档:http://www.oracle.com/technetwork/java/javase/manual-plugin-install-linux-136395.html
转自:http://www.cnblogs.com/yondy/archive/2013/01/02/2842171.html