`
julylin
  • 浏览: 47522 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

为解压版Tomcat创建一个windows服务(修正版)

    博客分类:
  • J2EE
阅读更多
转自http://haolll230sss.blog.163.com/blog/static/578106722008388240343/
    感谢原作者。


一.配置环境变量

1.把JDK安装到C:\Program Files\Java\jdk1.6.0,下载免安装版tomcat6.0(Tomcat Zip)压缩包,把Tomcat解压放到C:\Tomcat6.0下


2.添加系统环境变量:

JAVA_HOME=C:\Program Files\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 Apache Software Foundation (ASF) under one or more

rem contributor license agreements.  See the NOTICE file distributed with

rem this work for additional information regarding copyright ownership.

rem The ASF licenses this file to You 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 regarding copyright ownership.

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

rem (the "License"); you may not use this file 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,那么你一定要不"Apache tomcat"改成其它名字,而且不要以"Apache tomcat"为前缀,否则会由于你系统已经存在服务"Apache tomcat"而启动失败,可以把名字改为:"你的名字"+apache形式

set PR_DESCRIPTION=Apache Tomcat Server - 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 regarding copyright ownership.

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

rem (the "License"); you may not use this file 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 in writing, 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 language governing 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 using Tomcat6 as service name.

rem                        Service is installed using default settings.

rem remove                 Remove the service from the System.

rem

rem name        (optional) If the second argument is present it is considered

rem                        to be new service name                                          

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 not defined correctly.

echo This environment variable is needed to run this program

goto end

rem Make sure prerequisite environment variables are set

if not "%JAVA_HOME%" == "" goto okHome

echo The JAVA_HOME environment variable is not defined

echo This environment variable is needed to run this program

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//原来为 set SERVICE_NAME=Tomcat6,这里hao是我的名字

set PR_DISPLAYNAME=hao apache//原来为 set PR_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 --StopClass org.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 not needed 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 been installed

说明服务Tomcat已经被安装成功。

顺便说一下,运行service.bat remove可以移除服务。

3、到这里,服务添加成功,控制台下运行services.msc,可以看到添加的服务(在右边可以找到PR_DISPLAYNAME指定的服务名"hao apache"),默认状态下该服务是手动运行的,在他的属性中,可以将启动类型更改为“自动”,以后机器启动以后Tomcat就在后台启动了。



四 控制台控制服务的命令

启动服务

net Start 服务名(此服务名为set SERVICE_NAME后面指定的服务名,或者为set PR_DISPLAYNAME指定的,下面一样)

关闭服务

net stop 服务名

注:

1 此服务名可以是SERVICE_NAME,也可以是PR_DISPLAYNAME;

2 如果服务名中包含空格或者中文,请将服务名用半角双引号包含起来。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics