CentOS安装jdk的三种方法

前端之家收集整理的这篇文章主要介绍了CentOS安装jdk的三种方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

环境



Linux版本:CentOS 6.5、Ubuntu 12.04.5

JDK版本:JDK 1.7


目录



方法一:手动解压JDK的压缩包,然后设置环境变量

方法二:用yum安装JDK

方法三:用rpm安装JDK

方法四:Ubuntu 上使用apt-get安装JDK



内容




1.在/usr/目录下创建java目录

[root@localhost~]#mkdir/usr/java
[root@localhost~]#cd/usr/java

2.下载,然后解压

[root@localhostjava]#curl-Ohttp://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.tar.gz
[root@localhostjava]#tar-zxvfjdk-7u79-linux-x64.tar.gz

3.设置环境变量

[root@localhostjava]#vi/etc/profile

添加如下内容

#setjavaenvironment
JAVA_HOME=/usr/java/jdk1.7.0_79
JRE_HOME=/usr/java/jdk1.7.0_79/jre
CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
exportJAVA_HOMEJRE_HOMECLASS_PATHPATH

修改生效:

[root@localhostjava]#source/etc/profile

4.验证

[root@localhostjava]#java-version
javaversion"1.7.0_79"
Java(TM)SERuntimeEnvironment(build1.7.0_79-b15)
JavaHotSpot(TM)64-BitServerVM(build24.79-b02,mixedmode)


方法二:用yum安装JDK(CentOS)

1.查看yum库中都有哪些jdk版本

[root@localhost~]#yumsearchjava|grepjdk
ldapjdk-javadoc.x86_64:Javadocforldapjdk
java-1.6.0-openjdk.x86_64:OpenJDKRuntimeEnvironment
java-1.6.0-openjdk-demo.x86_64:OpenJDKDemos
java-1.6.0-openjdk-devel.x86_64:OpenJDKDevelopmentEnvironment
java-1.6.0-openjdk-javadoc.x86_64:OpenJDKAPIDocumentation
java-1.6.0-openjdk-src.x86_64:OpenJDKSourceBundle
java-1.7.0-openjdk.x86_64:OpenJDKRuntimeEnvironment
java-1.7.0-openjdk-demo.x86_64:OpenJDKDemos
java-1.7.0-openjdk-devel.x86_64:OpenJDKDevelopmentEnvironment
java-1.7.0-openjdk-javadoc.noarch:OpenJDKAPIDocumentation
java-1.7.0-openjdk-src.x86_64:OpenJDKSourceBundle
java-1.8.0-openjdk.x86_64:OpenJDKRuntimeEnvironment
java-1.8.0-openjdk-demo.x86_64:OpenJDKDemos
java-1.8.0-openjdk-devel.x86_64:OpenJDKDevelopmentEnvironment
java-1.8.0-openjdk-headless.x86_64:OpenJDKRuntimeEnvironment
java-1.8.0-openjdk-javadoc.noarch:OpenJDKAPIDocumentation
java-1.8.0-openjdk-src.x86_64:OpenJDKSourceBundle
ldapjdk.x86_64:TheMozillaLDAPJavaSDK

2.选择版本,进行安装

我们这里安装1.7版本

[root@localhost~]#yuminstalljava-1.7.0-openjdk

安装完之后,默认的安装目录是在: /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75.x86_64

[root@localhost~]#vi/etc/profile

#setjavaenvironment JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75.x86_64 JRE_HOME=$JAVA_HOME/jre CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin exportJAVA_HOMEJRE_HOMECLASS_PATHPATH

3.验证

同上。

注:因为采用yum安装jdk,系统考虑到多版本的问题,会用alternatives进行版本控制。开始,相应版本的jdk安装在/usr/lib/jvm/之后,会在alternatives中注册,在/etc/alternatives目录下会产生一些链接到/usr/lib/jvm/中刚安装好的jdk版本。

在/usr/bin下面会有链接到/etc/alternatives的相应的文件。比如,/usr/bin下面会有一个链接文件java的映射关系如下:

/usr/bin/java->/etc/alternatives/java
/etc/alternatives/java->/usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java

这样,java命令不用设置环境变量,就可以正常运行了。但如果对于tomcat或其他软件来说还是要设置环境变量。

同样,如果安装新的版本jdk,就会重新链接到最新安装的jdk版本。当然,也可以使用alternatives修改所要使用的版本。

具体方法可参照:使用Linuxalternatives管理多版本的软件



1.下载

[hadoop@localhost~]$curl-Ohttp://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.rpm

2.使用rpm命令安装

[root@localhost~]#rpm-ivhjdk-7u79-linux-x64.rpm
Preparing...###########################################[100%]
1:jdk###########################################[100%]
UnpackingJARfiles...
rt.jar...
jsse.jar...
charsets.jar...
tools.jar...
localedata.jar...
jfxrt.jar...

和yum安装类似,不用设置环境变量就可以,运行java命令。rpm安装方式默认会把jdk安装到/usr/java/jdk1.7.0_79,然后通过三层链接链接到/usr/bin,具体链接如下:

[root@localhost~]#cd/bin
[root@localhostbin]#ll|grepjava
lrwxrwxrwx.1rootroot25Mar2811:24jar->/usr/java/default/bin/jar
lrwxrwxrwx.1rootroot26Mar2811:24java->/usr/java/default/bin/java
lrwxrwxrwx.1rootroot27Mar2811:24javac->/usr/java/default/bin/javac
lrwxrwxrwx.1rootroot29Mar2811:24javadoc->/usr/java/default/bin/javadoc
lrwxrwxrwx.1rootroot28Mar2811:24javaws->/usr/java/default/bin/javaws
lrwxrwxrwx.1rootroot30Mar2811:24jcontrol->/usr/java/default/bin/jcontrol
[root@localhostbin]#cd/usr/java/
[root@localhostjava]#ll
total4
lrwxrwxrwx.1rootroot16Mar2811:24default->/usr/java/latest
drwxr-xr-x.8rootroot4096Mar2811:24jdk1.7.0_79
lrwxrwxrwx.1rootroot21Mar2811:24latest->/usr/java/jdk1.7.0_79

方法四:Ubuntu 上使用apt-get安装JDK


1.查看apt库都有哪些jdk版本

root@Itble:~#apt-cachesearchjava|grepjdk
default-jdk-StandardJavaorJavacompatibleDevelopmentKit
default-jdk-doc-StandardJavaorJavacompatibleDevelopmentKit(documentation)
gcj-4.6-jdk-gcjandclasspathdevelopmenttoolsforJava(TM)
gcj-jdk-gcjandclasspathdevelopmenttoolsforJava(TM)
openjdk-6-dbg-JavaruntimebasedonOpenJDK(debuggingsymbols)
openjdk-6-demo-JavaruntimebasedonOpenJDK(demosandexamples)
openjdk-6-doc-OpenJDKDevelopmentKit(JDK)documentation
openjdk-6-jdk-OpenJDKDevelopmentKit(JDK)
openjdk-6-jre-lib-OpenJDKJavaruntime(architectureindependentlibraries)
openjdk-6-source-OpenJDKDevelopmentKit(JDK)sourcefiles
openjdk-7-dbg-JavaruntimebasedonOpenJDK(debuggingsymbols)
openjdk-7-demo-JavaruntimebasedonOpenJDK(demosandexamples)
openjdk-7-doc-OpenJDKDevelopmentKit(JDK)documentation
openjdk-7-jdk-OpenJDKDevelopmentKit(JDK)
openjdk-7-source-OpenJDKDevelopmentKit(JDK)sourcefiles
uwsgi-plugin-jvm-openjdk-6-JavapluginforuWSGI(OpenJDK6)
uwsgi-plugin-jwsgi-openjdk-6-JWSGIpluginforuWSGI(OpenJDK6)
openjdk-6-jre-OpenJDKJavaruntime,usingHotspotJIT
openjdk-6-jre-headless-OpenJDKJavaruntime,usingHotspotJIT(headless)
openjdk-7-jre-OpenJDKJavaruntime,usingHotspotJIT
openjdk-7-jre-headless-OpenJDKJavaruntime,usingHotspotJIT(headless)
openjdk-7-jre-lib-OpenJDKJavaruntime(architectureindependentlibraries)

2.选择版本进行安装

root@Itble:~#apt-getinstallopenjdk-7-jdk

root@Itble:~#vi/etc/profile

#setjavaenvironment JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-amd64 JRE_HOME=$JAVA_HOME/jre CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin exportJAVA_HOMEJRE_HOMECLASS_PATHPATH

root@Itble:~#source/etc/profile

root@Itble:~#java-version javaversion"1.7.0_79" OpenJDKRuntimeEnvironment(IcedTea2.5.5)(7u79-2.5.5-0ubuntu0.12.04.1) OpenJDK64-BitServerVM(build24.79-b02,63); line-height:30px"> Ubuntu的apt-get安装方式和CentOS的yum安装方式很类似,这里就不再啰嗦。


from:http://www.mamicode.com/info-detail-613410.html

猜你在找的CentOS相关文章