CentOs5.5安装jdk1.6--史上最简单靠谱步骤

上一篇 / 下一篇  2010-12-16 10:47:16 / 天气: 冷 / 心情: 平静 / 精华(1) / 置顶(1) / 个人分类:Java

官方安装帮助
http://www.oracle.com/technetwork/java/javase/install-linux-rpm-137089.html?ssSourceSiteId=otncn
JDK RPM Installation for Linux (32-bit)         JDK Documentation
System Requirements
System Configurations

Installation Instructions
This procedure installs the Java SE Runtime Environment for 32-bit Linux, using an RPM binary bundle. Some Linux distributions, such as Ubuntu, are not RPM-based. In that event, please see the self extracting installation page.
The name of the downloaded file has the following format:
    jdk-6u
           
<version>-linux-i586-rpm.bin
         
<version>
    jdk-6u18-linux-i586-rpm.bin
To install, download this file and use the following instructions:

1. Download and check the file size.
You can download to any directory that you can write to.
2. Become root by running the su command and entering the root password.
3. Extract and install the contents of the downloaded file.
Change directory to where the downloaded file is located and run these commands to first set the executable permissions and then run the binary to extract and run the RPM file:

                
% chmod a+x jdk-6u
                 
<version>-linux-i586-rpm.bin
% ./jdk-6u
                 
<version>-linux-i586-rpm.bin
               
      
             
Note that the initial " ./" is required if you do not have " ." in your PATH environment variable.
The script. displays a binary license agreement, which you are asked to agree to before installation can proceed. Once you have agreed to the license, the install script. creates and runs the file jdk-6u <version>-linux-i586.rpm in the current directory.
NOTE: If you want to only extract the RPM file but not install it, you can run the .bin file with the -x argument. You do not need to be root to do this.
4. Delete the rpm file if you want to save disk space.
5. Exit the root shell.
6. If you want to use Java within the browser, setup the plugin using the instructions in Manual Plugin Installation for Linux.

The RPM package creates two links: /usr/java/latest and /usr/java/default.
The /usr/java/latest link will always point to the version of Java that is considered the latest version. Subsequent upgrades of the package will overwrite this value if it is not the latest version.

By default, /usr/java/default points to /usr/java/latest. However, if administrators change /usr/java/default to point to another version of Java, subsequent package upgrades will be provided by the administrators and cannot be overwritten.
When the JDK is installed, links to javac, jar, and javadoc are also created apart from the JRE links. These links point to the appropriate tool referenced by /usr/java/default. This allows the user to easily run the default version of these Java tools.
A new service script, named jexec, is added to /etc/init.d. This script. allows users to directly execute any standalone JAR file that has an execution permission set. This can be demonstrated using an example from the JDK:
        cd /usr/java/jdk1.6.0/demo/jfc/SwingSet2
        chmod +x SwingSet2.jar
        ./SwingSet2.jar
 
一.下载JDK
1)登录Sun的JDK官方下载网 址:http://java.sun.com/javase/downloads/index.jsp
2)下载jdk-6u22-linux-i586.bin文件
二.删除系统自带OpenJDK
  此步可不执行,参考第三步 5) 修改默认的JDK为jdk1.6 通过符号链接指向新的jdk 
  也可以直接参考第三步 4) 配置jdk1.6.0_22环境变量 该步最靠谱
CentOs5.5 默认会安装OpenOffice之类,需要JAVA支持,系统默认安装了一个openjdk1.6版本
# java -version
java version "1.6.0"
OpenJDK  Runtime Environment (build 1.6.0-b09)
OpenJDK Client VM (build 1.6.0-b09, mixed mode)

备注:
无须删除原有的openjdk;否则,与之相关的openoffice等软件,也会自动随之删除;
 

如果直接"应用程序-添加/删除软件--基本系统--java“删除其他相关软件也会自动删除,所以删除前,最好安装配置好新的JDK环境

1) 删除原有的JDK:
# alternatives –remove java /usr/share/java/libgcj-java-placeholder.sh
# alternatives --remove javac /usr/share/java/libgcj-javac-placeholder.sh

# alternatives --remove jar /usr/bin/fastjar
# alternatives --remove jar.1.gz /usr/share/man/man1/fastjar.1.gz
2) 删除原有的链接:
# cd /usr/bin
# rm -rf java
# rm –rf javac
3) 删除原有java目录
# rm -rf /usr/java/
 

三配置新的JDK环境
1)把安装文件文件保存在/usr/java/下 ,并切换到root用户
# mkdir /usr/java 创建/urs/java目录
# cp jdk-6u22-linux-i586.bin /usr/java/ 把安装文件复制到/usr/java
#su -
#输入root密码

2)添加可执行的权限
chmod 755 jdk-6u22-linux-i586.bin 或 chmod a+x jdk-6u22-linux-i586.bin

3) 安装jdk1.6

# ./jdk-6u22-linux-i586.bin #(大约9页 License) say yes to install jdk
完成后 /usr/java目录下会新建一个名“jdk1.6.0_22”的目录,至此我们已经在CentOS下安装好了新的jdk


4) 配置jdk1.6.0_22环境变量

查看 确认新安装的jdk1.6.0_22 的目录为 /usr/java/default

(英文安装帮助有说明,搞了半天直接该步修改好环境变量就可以成功,网上的步骤删除openjdk 又改符号链接都不靠谱)
# cd /usr/java
# ls -al
drwxr-xr-x  3 root root     4096 12-16 09:45 .
drwxr-xr-x 15 root root     4096 12-15 16:54 ..
lrwxrwxrwx  1 root root       16 12-15 16:55 default -> /usr/java/latest
drwxr-xr-x 10 root root     4096 12-16 09:46 jdk1.6.0_22
-rwxr-xr-x  1 root root 83761756 12-16 09:42 jdk-6u22-linux-i586.bin
lrwxrwxrwx  1 root root       21 12-15 16:55 latest -> /usr/java/jdk1.6.0_22

 
配置jdk1.6.0_22环境变量
# vi /etc/profile
export JAVA_HOME=/usr/java/default
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/lib:$JAVA_HOME/jre/bin:$PATH:$HOME/bin:$PATH
export JAVA_TOOLS=$JAVA_HOME/lib/tools.jar
使环境变量生效:
#source /etc/profile
5) 修改默认的JDK为jdk1.6
此步可略过参考第4)步后,就已经安装成功 该步是网络上有人如此设置比较麻烦且使用的环境变量更改也有不同
 
先看下默认的链接
[root@tm java]# cd /usr/bin
[root@tm bin]# ls -al|grep java
-rwxr-xr-x  1 root root        435 2006-11-22 antlr-java
lrwxrwxrwx  1 root root         25 12-15 16:55 jar -> /usr/java/default/bin/jar
lrwxrwxrwx  1 root root         22 12-06 09:37 java -> /etc/alternatives/java
lrwxrwxrwx  1 root root         27 12-15 16:55 javac -> /usr/java/default/bin/javac
lrwxrwxrwx  1 root root         29 12-15 16:55 javadoc -> /usr/java/default/bin/javadoc
lrwxrwxrwx  1 root root         28 12-15 16:55 javaws -> /usr/java/default/bin/javaws
lrwxrwxrwx  1 root root         30 12-15 16:55 jcontrol -> /usr/java/default/bin/jcontrol
[root@tm bin]# ls -al|grep javac
lrwxrwxrwx  1 root root         27 12-15 16:55 javac -> /usr/java/default/bin/javac

在/usr/bin下建立软链接java:
# ln -s /usr/java/jdk1.5.0_17/bin/java /usr/bin/java
# ln -s /usr/java/jdk1.5.0_17/bin/javac /usr/bin/javac
为了方便后面的工作,在/usr/lcoal下也建立两个软链接
# ln -s /usr/java/jdk1.5.0_17 /usr/local/jdk
# ln -s /usr/java/jdk1.5.0_17/jre/ /usr/local/jre
 
 
测试安装
   1)查看版本号
# java -version
java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
Java HotSpot(TM) Client VM (build 17.1-b03, mixed mode, sharing)

   2)使用VI编辑器编写一个Java程序
     (1)#root > vi Hello.java
     (2) 编写程序
        public class Hello
         {
            public static void main(String[] args)
         {
          System.out.println("Hello");
         }
        }
   3)编译:javac Hello.java
   4)执行:java Hello
 
写后小注:
其实注意看官方安装说明这几句:
The RPM package creates two links: /usr/java/latest and /usr/java/default.
The /usr/java/latest link will always point to the version of Java that is considered the latest version. Subsequent upgrades of the package will overwrite this value if it is not the latest version.
 
By default, /usr/java/default points to /usr/java/latest.
 
 
就可以知道 其实JAVA_HOME 设置为  /usr/java/default 就可以了
不用删掉老的openjdk 也不用费力改那么多符号链接
向官方帮助致敬!!!

TAG:

 

评分:0

我来说两句

Open Toolbar