一、yum安装
1.执行:yum search jdk
已加载插件:fastestmirror, security
Loading mirror speeds from cached hostfile
* base: mirror.bit.edu.cn
* extras: mirror.bit.edu.cn
* updates: mirror.bit.edu.cn
=================================== N/S Matched: jdk ====================================
java-1.6.0-openjdk.i686 : OpenJDK Runtime Environment
java-1.6.0-openjdk-demo.i686 : OpenJDK Demos
java-1.6.0-openjdk-devel.i686 : OpenJDK Development Environment
java-1.6.0-openjdk-javadoc.i686 : OpenJDK API Documentation
java-1.6.0-openjdk-src.i686 : OpenJDK Source Bundle
java-1.7.0-openjdk.i686 : OpenJDK Runtime Environment
java-1.7.0-openjdk-demo.i686 : OpenJDK Demos
java-1.7.0-openjdk-devel.i686 : OpenJDK Development Environment
java-1.7.0-openjdk-javadoc.noarch : OpenJDK API Documentation
java-1.7.0-openjdk-src.i686 : OpenJDK Source Bundle
java-1.8.0-openjdk.i686 : OpenJDK Runtime Environment
java-1.8.0-openjdk-debug.i686 : OpenJDK Runtime Environment with full debug on
java-1.8.0-openjdk-demo.i686 : OpenJDK Demos
java-1.8.0-openjdk-demo-debug.i686 : OpenJDK Demos with full debug on
java-1.8.0-openjdk-devel.i686 : OpenJDK Development Environment
java-1.8.0-openjdk-devel-debug.i686 : OpenJDK Development Environment with full debug on
java-1.8.0-openjdk-headless.i686 : OpenJDK Runtime Environment
java-1.8.0-openjdk-headless-debug.i686 : OpenJDK Runtime Environment with full debug on
java-1.8.0-openjdk-javadoc.noarch : OpenJDK API Documentation
java-1.8.0-openjdk-javadoc-debug.noarch : OpenJDK API Documentation for packages with
: debug on
java-1.8.0-openjdk-src.i686 : OpenJDK Source Bundle
java-1.8.0-openjdk-src-debug.i686 : OpenJDK Source Bundle for packages with debug on
ldapjdk-javadoc.i686 : Javadoc for ldapjdk
icedtea-web.i686 : Additional Java components for OpenJDK - Java browser plug-in and Web
: Start implementation
ldapjdk.i686 : The Mozilla LDAP Java SDK
2.选择合适版本,执行yum install -y java-1.7.0*
3.测试是否成功安装:java -version
此方法安装完就自动配置好环境变量(其实是将java的bin程序放到系统目录,类库也放到常用系统库)
二、源码安装
1、源码包准备:
首先到官网下载jdk,http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html,我选择
jdk-7u79-linux-i586.tar.gz 下载到当前主目录(其他地方也可以自己决定)
#pwd
/root
#wget http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-i586.tar.gz?AuthParam=1479978137_d0f11c141a1be6fb1488227dff44be09
如果下载不下来,可以试试其他方法。
#ls -al 看下tar包属性。
2、解压源码包,并放到适当位置
解压压缩包,命令行:
# tar xzvf jdk-7u79-linux-i586.tar.gz
然后可以把压缩包删除,命令行:
# rm jdk-7u79-linux-i586.tar.gz
将解压的文件夹放到/usr/local中
#mv jdk1.7.0_45 /usr/loca
进入/usr/local 并创建链接
#cd /usr/local
#ln -s /usr/local/jdk1.7.0_45 java
3、设置jdk环境变量
这里采用全局设置方法,就是修改etc/profile,它是是所有用户的共用的环境变量
#vi /etc/profile
打开之后在末尾添加
export JAVA_HOME=/usr/local/java export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/jre/lib
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
请记住,在上述添加过程中,等号两侧不要加入空格,不然会出现“不是有效的标识符”,因为source /etc/profile 时不能识别多余到空格,会理解为是路径一部分。
然后保存
# source /etc/profile 使profile生效
4、检验是否安装成功
在终端
java -version
看看是否安装成功。