JDK安装
安装系统时候默认安装了jdk,这里我先卸载了,卸载时候千万别用Yum remove,使用rpm -e --nodeps
卸载:
[root@localhost ~]# rpm -qa|grep gcj [root@localhost ~]# rpm -qa|grep java tzdata-java-2013g-1.el6.noarch java-1.6.0-openjdk-1.6.0.0-1.66.1.13.0.el6.i686 java-1.7.0-openjdk-1.7.0.45-2.4.3.3.el6.i686 [root@localhost ~]# rpm -qa|grep jdk java-1.6.0-openjdk-1.6.0.0-1.66.1.13.0.el6.i686 java-1.7.0-openjdk-1.7.0.45-2.4.3.3.el6.i686 [root@localhost ~]# rpm -e --nodeps java-1.6.0-openjdk-1.6.0.0-1.66.1.13.0.el6.i686 [root@localhost ~]# rpm -e --nodeps java-1.7.0-openjdk-1.7.0.45-2.4.3.3.el6.i686 [root@localhost ~]# rpm -qa|grep jdk [root@localhost ~]# java -version -bash: /usr/bin/java: No such file or directory [root@localhost ~]#
安装:
下载地址:https://jdk6.java.net/download.html;jdk有bin和rpm两种包,
[root@localhost ~]# getconf LONG_BIT 32 [root@localhost ~]# cat /proc/cpuinfo |grep flags|grep "lm"|wc -l 2 [root@localhost ~]#
根据自己的系统版本为32、64位,下载相应的版本,我的系统当前运行的是32位的,cpu支持64位,下载32位版本上传至服务器,并赋予可执行权限;
[root@localhost src]# ls jdk-6u38-ea-bin-b04-linux-i586-31_oct_2012.bin [root@localhost src]# chmod 755 jdk-6u38-ea-bin-b04-linux-i586-31_oct_2012.bin [root@localhost src]# ./jdk-6u38-ea-bin-b04-linux-i586-31_oct_2012.bin [root@localhost src]# ll total 196736 drwxr-xr-x 8 root root 4096 Sep 10 03:54 jdk1.6.0_38 -rwxr-xr-x 1 root root 71771776 Sep 10 2014 jdk-6u38-ea-bin-b04-linux-i586-31_oct_2012.bin [root@localhost usr]# java -version -bash: java: command not found [root@localhost usr]#
至此jdk安装完成,如果是.gz形式,直接tar解压即可。
[root@localhost src]#tar zxvf jdk-7u79-linux-x64.gz
我们还需要设置环境变量,设置环境有好几种,这里讲3种:
第一种:只对当前shell生效。如果从另外一个shell登陆,将不能使用刚才设置的变量
[root@localhost src]# export JAVA_HOME=/usr/local/src/jdk1.6.0_38 [root@localhost src]# export JAVA_BIN=/usr/local/src/jdk1.6.0_38/bin [root@localhost src]# export PATH=$PATH:$JAVA_HOME/bin [root@localhost src]# export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar [root@localhost src]# export JAVA_HOME JAVA_BIN PATH CLASSPATH [root@localhost jdk1.6.0_38]# java -version java version "1.6.0_38-ea" Java(TM) SE Runtime Environment (build 1.6.0_38-ea-b04) Java HotSpot(TM) Client VM (build 20.13-b02, mixed mode, sharing) [root@localhost jdk1.6.0_38]#
第二种:将对所有用户的shell都生效,对系统安全会产生影响
[root@localhost src]# vim /etc/profile ;文末添加以下 export JAVA_HOME=/usr/local/src/jdk1.6.0_38 export JAVA_BIN=/usr/local/src/jdk1.6.0_38/bin export PATH=$PATH:$JAVA_HOME/bin export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export JAVA_HOME JAVA_BIN PATH CLASSPATH [root@localhost src]# source /etc/profile ;刷新立即生效 [root@localhost src]# java -version java version "1.6.0_38-ea" Java(TM) SE Runtime Environment (build 1.6.0_38-ea-b04) Java HotSpot(TM) Client VM (build 20.13-b02, mixed mode, sharing) [root@localhost src]#
第三种:针对某一用户的shell生效,修改该用户主目录下的.barshrc文件
[root@localhost src]# vim /root/.bashrc ;文末添加以下 export JAVA_HOME=/usr/local/src/jdk1.6.0_38 export JAVA_BIN=/usr/local/src/jdk1.6.0_38/bin export PATH=$PATH:$JAVA_HOME/bin export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export JAVA_HOME JAVA_BIN PATH CLASSPATH [root@localhost src]# source /root/.bashrc [root@localhost src]# java -version java version "1.6.0_38-ea" Java(TM) SE Runtime Environment (build 1.6.0_38-ea-b04) Java HotSpot(TM) Client VM (build 20.13-b02, mixed mode, sharing) [root@localhost src]#
我们来写个测试文件来测试下java是否能够正常使用
[root@localhost src]# vim test.java class test { public static void main(String[] args) { System.out.println("Justin Peng!!!"); } } [root@localhost src]# javac test.java ;编译 [root@localhost src]# java test ;执行 Justin Peng!!! [root@localhost src]#
OK,可以正常使用,至此JDK正式完成!!!
如果下载的rpm类型的
[root@localhost src]# ls jdk-7u75-linux-i586.rpm [root@localhost src]# rpm -ivh jdk-7u75-linux-i586.rpm Preparing... ########################################### [100%] 1:jdk ########################################### [100%] Unpacking JAR files... rt.jar... jsse.jar... charsets.jar... tools.jar... localedata.jar... jfxrt.jar... plugin.jar... javaws.jar... deploy.jar... [root@localhost src]# rpm -ql jdk ;查看生成文件位置
Tomcat安装
tomcat不用怎么安装,只需要解压下载的软件包即可使用
下载地址:http://tomcat.apache.org/download-60.cgi
[root@localhost src]# ls apache-tomcat-6.0.41.tar.gz jdk-6u38-ea-bin-b04-linux-i586-31_oct_2012.bin test.java jdk1.6.0_38 test.class [root@localhost src]# tar zxvf apache-tomcat-6.0.41.tar.gz -C /usr/local/ [root@localhost src]# cd !$ cd /usr/local/ [root@localhost local]# ls apache-tomcat-6.0.41 bin etc games include lib libexec sbin share src [root@localhost local]# mv apache-tomcat-6.0.41 tomcat [root@localhost bin]# ./startup.sh ;启动tomcat Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/src/jdk1.6.0_38 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar [root@localhost bin]# pwd /usr/local/tomcat/bin [root@localhost bin]#
设置tomcat开机启动:
[root@localhost local]# cp tomcat/bin/catalina.sh /etc/init.d/tomcat [root@localhost local]# vim /etc/init.d/tomcat ;在文件开始位置添加2、3、4、5行内容 1 #!/bin/sh 2 # chkconfig: 2345 10 90 3 # description:Tomcat service 4 CATALINA_HOME=/usr/local/tomcat 5 JAVA_HOME=/usr/local/src/jdk1.6.0_38 [root@localhost local]# chkconfig --add tomcat [root@localhost local]# chkconfig tomcat on
第二行是服务的配置:第一个数字是服务的运行级,2345表明这个服务的运行级是2、3、4和5级(Linux的运行级为0到6);第二个数字是启动优先级,数值从0到99;第三个数是停止优先级,数值也是从0到99。
第三行是对服务的描述,(确认2、3行不要拼写错误,否则在执行“chkconfig --add tomcat”时,会出现“tomcat服务不支持chkconfig”的错误提示
这样,tomcat在开启后会自动启动。
其实catalina.sh调用的是setclasspath.sh,如果我们把上面86、87行注释掉会提示调用的setclasspath.sh
[root@localhost ~]# service tomcat start Cannot find /etc/bin/setclasspath.sh This file is needed to run this program [root@localhost ~]#
如果我们只注释掉87行,即只指定setclasspath.sh路径,不指定jdk路径,出现以下提示
[root@localhost ~]# service tomcat start Neither the JAVA_HOME nor the JRE_HOME environment variable is defined At least one of these environment variable is needed to run this program [root@localhost ~]#
意思即不在java_home也不在jre_home环境变量中,至少需要定义一个环境变量来运行这个程序,此时只要在setclasspath.sh的开头声明环境变量(21 22行)
[root@localhost ~]# vim /usr/local/tomcat/bin/setclasspath.sh 20 # ----------------------------------------------------------------------------- 21 export JAVA_HOME=/usr/local/src/jdk1.6.0_38 22 export JRE_HOME=/usr/local/src/jdk1.6.0_38/jre 23 # Make sure prerequisite environment variables are set 24 if [ -z "$JAVA_HOME" -a -z "$JRE_HOME" ]; then [root@localhost ~]# service tomcat start Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/src/jdk1.6.0_38/jre Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar [root@localhost ~]#
windows中操作同以上,只是配置文件是.bat的,同时export也替换成set
默认tomcat的端口是8080,直接浏览器里输入http://localhost:8080就可以看到tomcat的信息页面,如果想修改Tomcat的端口(例如改8080端口为80端口),就用vi打开Tomcat目录下的/conf/server.xml
[root@localhost ~]# vim /usr/local/tomcat/conf/server.xml 69 # <Connector port="8080" protocol="HTTP/1.1" 70 <Connector port="80" protocol="HTTP/1.1" 71 connectionTimeout="20000" 72 redirectPort="8443" /> 73 <!-- A "Connector" using the shared thread pool-->
修改完成后需要reboot
查看tomcat版本信息:
[root@localhost bin]# ./version.sh Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/java/jdk1.7.0_75 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar Server version: Apache Tomcat/6.0.41 Server built: May 19 2014 11:49:25 Server number: 6.0.41.0 OS Name: Linux OS Version: 2.6.32-431.el6.i686 Architecture: i386 JVM Version: 1.7.0_75-b13 JVM Vendor: Oracle Corporation [root@localhost bin]# pwd /usr/local/tomcat/bin [root@localhost bin]#