解压版Tomcat创建服务启动使用方法



解压版Tomcat创建服务启动使用方法。

一.配置环境变量
1.把JDK安装到C:\Program Files\Java\jdk1.6.0,下载免安装版tomcat6.0(Tomcat Zip)压缩包,把Tomcat解压放到C:\Tomcat6.0下
2.添加系统环境变量:
JAVA_HOME=C:\ProgramFiles\Java\jdk1.6.0
CLASSPATH=.;%JAVA_HOME%\lib
TOMCAT_HOME=C:\Tomcat6.0
修改系统环境变量PATH,在它的最前面加入:
%JAVA_HOME%\bin;
注:放在最前面是预防有其它的JDK或JRE路径占先,如你装了Oracle就会有一个低版本的JRE。
二配置Tomcat启动环境(注意,如果已经象前面那样设置过了,这里就可以跳过了)
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
:o kHome
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 如果服务名中包含空格或者中文,请将服务名用半角双引号包含起来。

http://blog.csdn.net/songylwq/article/details/7416557