---
title: "Linux下安装JDK和Tomcat"
author: "greatrich"
date: "2012-05-21T23:22:16Z"
tags: ["JDK", "Linux", "Tomcat"]
categories: ["互联网"]
slug: "linux-jdk-tomcat"
draft: false
lastmod: "2012-05-21T23:28:25Z"
id: 1537013
url: "https://greatrich.iteye.com/blog/1537013"
---

安装准备：下载 jdk-6u23-linux-i586.bin

jdk下载路径http://www.oracle.com/technetwork/java/javase/downloads/jdk-6u32-downloads-1594644.html

apache-tomcat-7.0.12.tar.gz 文件

 

请根据自己的系统版本为32 、64 位，下载相应的版本

查看、卸载系统自带jdk

 

```
[root@localhost ~ ]#rpm –qa |grep gcj
java-1.5.0-gcj-1.5.0.0-29.1.el6.i686
libgcj-4.4.4-13.el6.i686 
```

卸载jdk

 

```
[root@localhost ~ ]#rpm –e java-1.5.0-gcj-1.5.0.0-29.1.el6.i686 --nodeps
[root@localhost ~ ]#rpm –e libgcj-4.4.4-13.el6.i686 --nodeps 
```

查看jdk 版本

```
[root@localhost ~]# java --version
```

 

检测javac 命令

```
[root@localhost ~]#javac
```

 

开始安装:

1 ．复制下载文件到/opt 目录下

```
[root@localhost ~ ]# cp jdk-6u23-linux-i586.bin apache-tomcat-7.0.12.tar.gz  /opt
```

 

2\. 赋予 jdk-6u23-linux-i586.bin 文件可执行权限

```
[root@localhost ~ ]# chmod 777 jdk-6u23-linux-i586.bin
```

 

3\. 编译二进制文件jdk-6u23-linux-i586.bin

```
[root@localhost ~ ]#./ jdk-6u23-linux-i586.bin
```

 

4\. 编译完成，生成jdk1.6.0\_23 文件，编辑/etc/profile, 在文件末尾添加jdk 路径, 重新编译/etc/profile 文件

```
[root@localhost ~ ]#vi /etc/profile
########## 加入如下内容
export JAVA_HOME=/opt/jdk1.6.0_23
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$CLASSPATH
##########
[root@localhost ~ ]# source /etc/profile 
```

5\. 解压apache-tomcat-7.0.12.tar.gz 文件。

```
[root@localhost opt]#tar xvfz apache-tomcat-7.0.12.tar.gz
```

 

6\. 解压后发现如下目录apache-tomcat-7.0.12 ，重命名为tomcat

```
[root@localhost ~ ]#mv apache-tomcat-7.0.12 tomcat
```

 

7\. 到tomcat 子目录bin 目录下找到catalina.sh 文件。复制文件到/etc/init.d 并改名为tomcat

 

```
[root@localhost ~ ]#cd /opt/tomcat/bin/
[root@localhost ~ ]#cp catalina.sh /etc/init.d/tomcat 
```

8\. 编辑/etc/init.d/tomcat 文件. 在文件中添加如下文中红色部分，蓝色为方便查找做的标注。下列为tomcat 部分内容，不可直接使用。

 

```
[root@localhost ~ ]#vi /etc/init.d/tomcat

#!/bin/sh

# chkconfig: 2345 10 90

# description:Tomcat service

# Licensed to the Apache Software Foundation (ASF) under one or more

# contributor license agreements.  See the NOTICE file distributed with

# this work for additional information regarding copyright ownership.

# The ASF licenses this file to You under the Apache License, Version 2.0

# (the "License"); you may not use this file except in compliance with

# the License.  You may obtain a copy of the License at

#

#     http://www.apache.org/licenses/LICENSE-2.0

#

#

# $Id: catalina.sh 1073891 2011-02-23 19:23:59Z markt $

#

 

-----------------------------------------------------------------------------

CATALINA_HOME=/opt/tomcat

JAVA_HOME=/opt/jdk1.6.0_23

 

 

# OS specific support.   $var _must_ be set to either true or false.

cygwin=false

os400=false

darwin=false

case "`uname`" in

CYGWIN*) cygwin=true;;

OS400*) os400=true;;

Darwin*) darwin=true;;

esac 
```

9\. 添加tomcat 服务

```
[root@localhost ~ ]#chkconfig --add tomcat 
```

 

10\. 关闭和启动tomcat 服务，由此生成的tomcat 没有重启命令

 

```
[root@localhost ~ ]#service tomcat stop      #### 关闭tomcat 服务
[root@localhost ~ ]#service tomcat start      #### 启动tomcat 服务 
```

11\. 查看tomcat 服务运行级别

```
[root@localhost ~ ]# chkconfig --list |grep tomcat
```

 

tomcat          0: 关闭  1: 关闭  2: 启用  3: 启用  4: 启用  5: 启用  6: 关闭

12\. 如果345 为关闭状态。执行如下命令设置tomcat 为开机自启动。

 

```
[root@localhost ~ ]# chkconfig tomcat on
```