一.配置環境變量java
1.把JDK安裝到C:\Program Files\Java\jdk1.6.0,下載免安裝版tomcat6.0(Tomcat Zip)壓縮包,把Tomcat解壓放到C:\Tomcat6.0下express
2.添加系統環境變量:apache
JAVA_HOME=C:\ProgramFiles\Java\jdk1.6.0bootstrap
CLASSPATH=.;%JAVA_HOME%\libtomcat
TOMCAT_HOME=C:\Tomcat6.0服務器
修改系統環境變量PATH,在它的最前面加入:app
%JAVA_HOME%\bin;less
注:放在最前面是預防有其它的JDK或JRE路徑佔先,如你裝了Oracle就會有一個低版本的JRE。jvm
二配置Tomcat啓動環境(注意,若是已經象前面那樣設置過了,這裏就能夠跳過了)ide
1、修改startup.bat文件:
在第一行前面加入以下兩行--
SET JAVA_HOME=JDK目錄
SET CATALINA_HOME=前面解壓後Tomcat的目錄
片斷以下:
@echo off
SET JAVA_HOME=C:\Program Files\Java\jdk1.6.0
SET CATALINA_HOME=C:\Tomcat6.0
rem Licensed to the ApacheSoftware Foundation (ASF) under one or more
rem contributor licenseagreements. See the NOTICE filedistributed with
rem this work for additionalinformation regarding copyright ownership.
rem The ASF licenses this file toYou under the Apache License, Version 2.0
rem (the "License");you may not use this file except in compliance with
2.若是須要使用shutdown.bat關閉服務器的話,也按照上面加入兩行。
片斷以下:
@echo off
SET JAVA_HOME=C:\Program Files\Java\jdk1.6.0
SET CATALINA_HOME=C:\Tomcat6.0
rem Licensed to the Apache Software Foundation (ASF)under one or more
rem contributor license agreements. See the NOTICE file distributed with
rem this work for additional information regardingcopyright ownership.
rem The ASF licenses this file to You under the ApacheLicense, Version 2.0
rem (the "License"); you may not use thisfile except in compliance with
rem the License. You may obtain a copy of the License at
rem
3、這樣,運行startup.bat就能夠運行服務器,運行shutdown.bat就能夠關閉服務器了。
可是這樣運行startup.bat後不能把startup.bat彈出的窗口關掉,不然就退出了tomcat6.0服務,因此爲了使用方便咱們能夠考慮把tomcat6.0加入系統的服務中去
三將Tomcat加入服務
//說明:若是你已經按照第一步(即一.配置環境變量),且運行startup.bat和shutdown.bat能成功那麼下面第一就不要作了,不然會出現錯誤
1、修改bin目錄中的service.bat:
REM 添加下面的一行
set CATALINA_HOME=%cd%
若是歷來沒有安裝過Tomcat,或者保證Services.msc啓動服務管理器檢查沒有Apache Tomcat系統服務,到此你就能夠轉到第二步了。不然繼續往下走--
//第一步通常不要了,不然可能出錯!!
REM 按照描述修改下面的幾行
set SERVICE_NAME=Tomcat6
REM 上面一行,Tomcat6修改爲你須要的服務名,這個將是一後使用net start/stop來操做的服務名稱。
//注意這一步很重要,若是你的系統如今有安裝tomcat6.0,那麼你必定要把"tomcat6"改爲其它名字,並且不要以"tomcat6"爲前綴,不然會因爲你係統已經存在服務"tomcat6"而啓動失敗,能夠把名字改成:"你的名字"+Tomcat6形式
set PR_DISPLAYNAME=Apache Tomcat
REM 上面一行,Apache Tomcat改成你須要的顯示服務名,這個將顯示在服務管理器中。
//注意這一步也是很重要,若是你的系統如今有安裝tomcat6.0,那麼你必定要不"Apachetomcat"改爲其它名字,並且不要以"Apachetomcat"爲前綴,不然會因爲你係統已經存在服務"Apache tomcat"而啓動失敗,能夠把名字改成:"你的名字"+apache形式
set PR_DESCRIPTION=Apache TomcatServer - http://jakarta.apache.org/tomcat
REM 這一行改不改無所謂,是服務的描述,根據本身的喜愛決定吧。
個人tomcat6.0中bin下的service.bat(下載後沒有通過其它的配置修改,保持下載時的狀態)修改後以下:貼出來以下
@echo off
rem Licensed to the Apache Software Foundation (ASF)under one or more
rem contributor license agreements. See the NOTICE file distributed with
rem this work for additional information regardingcopyright ownership.
rem The ASF licenses this file to You under the ApacheLicense, Version 2.0
rem (the "License"); you may not use thisfile except in compliance with
rem the License. You may obtain a copy of the License at
rem
rem http://www.apache.org/licenses/LICENSE-2.0
rem
rem Unless required by applicable law or agreed to inwriting, software
rem distributed under the License is distributed on an"AS IS" BASIS,
rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.
rem See the License for the specific languagegoverning permissions and
rem limitations under the License.
if "%OS%" == "Windows_NT" setlocal
rem---------------------------------------------------------------------------
rem NT Service Install/Uninstall script
rem
rem Options
rem install Install the service usingTomcat6 as service name.
rem Service is installedusing default settings.
rem remove Remove the service from theSystem.
rem
rem name (optional) If the second argument is present it is considered
rem to be new servicename
rem
rem $Id: service.bat 600659 2007-12-03 20:15:09Z jim $
rem---------------------------------------------------------------------------
rem Guess CATALINA_HOME if not defined
set CURRENT_DIR=%cd%
if not "%CATALINA_HOME%" == ""goto gotHome
set CATALINA_HOME=%cd%
if exist "%CATALINA_HOME%\bin\tomcat6.exe"goto okHome
rem CD to the upper dir
cd ..
set CATALINA_HOME=%cd%
:gotHome
if exist "%CATALINA_HOME%\bin\tomcat6.exe"goto okHome
echo The tomcat.exe was not found...
echo The CATALINA_HOME environment variable is notdefined correctly.
echo This environment variable is needed to run thisprogram
goto end
rem Make sure prerequisite environment variables are set
if not "%JAVA_HOME%" == "" gotookHome
echo The JAVA_HOME environment variable is not defined
echo This environment variable is needed to run thisprogram
goto end
:okHome
if not "%CATALINA_BASE%" == ""goto gotBase
set CATALINA_BASE=%CATALINA_HOME%
:gotBase
set EXECUTABLE=%CATALINA_HOME%\bin\tomcat6.exe
rem Set default Service name
set SERVICE_NAME=haotomcat6//原來爲 setSERVICE_NAME=Tomcat6,這裏hao是個人名字
set PR_DISPLAYNAME=hao apache//原來爲 setPR_DISPLAYNAME=Apache Tomcat
if "%1" == "" goto displayUsage
if "%2" == "" goto setServiceName
set SERVICE_NAME=%2
set PR_DISPLAYNAME=Apache Tomcat %2
:setServiceName
if %1 == install goto doInstall
if %1 == remove goto doRemove
if %1 == uninstall goto doRemove
echo Unknown parameter "%1"
:displayUsage
echo.
echo Usage: service.bat install/remove [service_name]
goto end
:doRemove
rem Remove the service
"%EXECUTABLE%" //DS//%SERVICE_NAME%
echo The service '%SERVICE_NAME%' has been removed
goto end
:doInstall
rem Install the service
echo Installing the service '%SERVICE_NAME%' ...
echo Using CATALINA_HOME: %CATALINA_HOME%
echo Using CATALINA_BASE: %CATALINA_BASE%
echo Using JAVA_HOME: %JAVA_HOME%
rem Use the environment variables as an example
rem Each command line option is prefixed with PR_
set PR_DESCRIPTION=Apache Tomcat Server -http://tomcat.apache.org/
set PR_INSTALL=%EXECUTABLE%
set PR_LOGPATH=%CATALINA_BASE%\logs
set PR_CLASSPATH=%CATALINA_HOME%\bin\bootstrap.jar
rem Set the server jvm from JAVA_HOME
set PR_JVM=%JAVA_HOME%\jre\bin\server\jvm.dll
if exist "%PR_JVM%" goto foundJvm
rem Set the client jvm from JAVA_HOME
set PR_JVM=%JAVA_HOME%\jre\bin\client\jvm.dll
if exist "%PR_JVM%" goto foundJvm
set PR_JVM=auto
:foundJvm
echo Using JVM: %PR_JVM%
"%EXECUTABLE%" //IS//%SERVICE_NAME%--StartClass org.apache.catalina.startup.Bootstrap --StopClassorg.apache.catalina.startup.Bootstrap --StartParams start --StopParams stop
if not errorlevel 1 goto installed
echo Failed installing '%SERVICE_NAME%' service
goto end
:installed
rem Clear the environment variables. They are notneeded any more.
set PR_DISPLAYNAME=
set PR_DESCRIPTION=
set PR_INSTALL=
set PR_LOGPATH=
set PR_CLASSPATH=
set PR_JVM=
rem Set extra parameters
"%EXECUTABLE%" //US//%SERVICE_NAME%--JvmOptions"-Dcatalina.base=%CATALINA_BASE%;-Dcatalina.home=%CATALINA_HOME%;-Djava.endorsed.dirs=%CATALINA_HOME%\endorsed"--StartMode jvm --StopMode jvm
rem More extra parameters
set PR_LOGPATH=%CATALINA_BASE%\logs
set PR_STDOUTPUT=auto
set PR_STDERROR=auto
"%EXECUTABLE%" //US//%SERVICE_NAME%++JvmOptions"-Djava.io.tmpdir=%CATALINA_BASE%\temp;-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager;-Djava.util.logging.config.file=%CATALINA_BASE%\conf\logging.properties"--JvmMs 128 --JvmMx 256
echo The service '%SERVICE_NAME%' has been installed.
:end
cd %CURRENT_DIR%
//service.bat中要修改的兩處地方已經在上面要紅色標出來了
2、運行cmd打開控制檯,進入Tomat目錄/bin文件夾,輸入以下命令運行。
service.bat install
程序提示:The service 'Tomcat5(或者你修改一後的SERVICE_NAME)' has beeninstalled
說明服務Tomcat已經被安裝成功。
順便說一下,運行service.bat remove能夠移除服務。
3、到這裏,服務添加成功,控制檯下運行services.msc,能夠看到添加的服務(在右邊能夠找到PR_DISPLAYNAME指定的服務名"haoapache"),默認狀態下該服務是手動運行的,在他的屬性中,能夠將啓動類型更改成「自動」,之後機器啓動之後Tomcat就在後臺啓動了。
四控制檯控制服務的命令
啓動服務
net Start 服務名(此服務名爲set SERVICE_NAME後面指定的服務名,或者爲setPR_DISPLAYNAME指定的,下面同樣)
關閉服務
net stop 服務名
注:
1 此服務名能夠是SERVICE_NAME,也能夠是PR_DISPLAYNAME;
2 若是服務名中包含空格或者中文,請將服務名用半角雙引號包含起來。
最後,預祝第一次配置順利。