到Sun网站下载自解压安装包jdk-xxxx.bin(我的是jdk1.5.0_16-linux-i586.bin)
安装:
将建立/usr/local/jdk目录
将jdk安装文件cp到/usr/local/jdk目录
[root@RHEL5 ]# chmod +x jdk-xxxx.bin
[root@RHEL5 # ./jdk-xxxx.bin
配置Java环境变量:
在 ~/.bash_profile 里加入下列几行环境变量即可:(注意下面参数的大小写)
export JAVA_HOME=/usr/local/jdk/jdk1.5.0_16(这个和windows的环境变量一样是Java的安装目录)
export PATH=$PATH:$JAVA_HOME/bin
export MANPATH=$JAVA_HOME/man
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/jre/lib/rt.jar
然后执行下列操作:
mv /usr/bin/java /usr/bin/java.bak
ln -s /usr/local/jdk/bin/java /usr/bin/java
注销当前用户,重新登录。
下面验证Java环境变量是否生效:
[root@RHEL5 ~]# echo $JAVA_HOME
/usr/local/jdk
[root@RHEL5 ~]# echo $CLASSPATH
.:/usr/local/jdk/lib/dt.jar:/usr/local/jdk/lib/tools.jar:/usr/local/jdk/jre/lib/rt.jar
[root@RHEL5 ~]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/jdk/bin:/home/lwg/bin:/usr/local/jdk/bin:/usr/local/jdk/bin
[root@RHEL5 ~]# java -version
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)
[root@RHEL5 ~]#
环境变量设置OK!
下面测试JDK:
[root@RHEL5 share]# vim test.java
class test{
public static void main(String[] args){
System.out.println("Hello World!");
}
}
保存,退出,然后编译、执行:
[root@RHEL5 share]javac test.java
[root@RHEL5 share]java test
Hello World!
好了,JDK工作正常!
如果要使某个用户具有运行java命令的权限,只要修改其bash初始化文件.bashrc或者.bash_profile即可。