startup.bat中关键设置语句:
@echo off //设置在echo off后面的命令都不显示在命令行上
//rem 表示注释
if "%OS%" == "Windows_NT" setlocal // 检查os环境变量的值是否等于Windows_NT,setlocal命令之后对环境变量的改变只对该批处理文件当次运行有影响;
set CURRENT_DIR=%cd%
if not "%CATALINA_HOME%" == "" goto gotHome //检查是否设置了CATALINA_HOME环境变量是否已经设置,如果设置了则调转到gotHome标记处
set CATALINA_HOME=%CURRENT_DIR% //否则将CATALINA_HOME环境变量设置为startup.bat启动执行时的工作目录
if exist "%CATALINA_HOME%/bin/catalina.bat" goto okHome //检查在CATALINA_HOME环境变量下是否存在/bin/catalina.bat,如果已经存在则跳转到okHome
cd .. //否则将CATALINA_HOME环境变量设置为startup.bat启动执行时的工作目录的上级目录;
set CATALINA_HOME=%cd%
cd %CURRENT_DIR%
:gotHome
if exist "%CATALINA_HOME%/bin/catalina.bat" goto okHome
echo The CATALINA_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program
goto end
:okHome
set EXECUTABLE=%CATALINA_HOME%/bin/catalina.bat //EXECUTABLE环境变量指向catalina.bat批处理程序,由此可知,Tomcat调用startup的时候实际上调用的是catalina.bat
if exist "%EXECUTABLE%" goto okExec
echo Cannot find %EXECUTABLE%
echo This file is needed to run this program
goto end
:okExec
set CMD_LINE_ARGS= //清除环境变量原来的值
:setArgs
if ""%1""=="""" goto doneSetArgs //检查命令行第一个参数值是否为空
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1 // 将命令行第一个参数值追加到CMD_LINE_ARGS环境变量上,个个参数以空格相连
shift //将命令行参数依次前移
goto setArgs
:doneSetArgs
call "%EXECUTABLE%" start %CMD_LINE_ARGS% //调用catalina.bat,首先给它传递start参数,然后将原来传送给startup.bat的参数CMD_LINE_ARGS传给catalina.bat;
:end
//在shutdown.bat程序中,传递给catalina.bat的第一个参数是stop;
call "%EXECUTABLE%" stop %CMD_LINE_ARGS%
//在catalina.bat程序中的关键设置:
if exist "%CATALINA_HOME%/bin/setenv.bat" call "%CATALINA_HOME%/bin/setenv.bat" //检查CATALINA_HOME目录下面是否存在/bin/setenv.bat批处理程序,如果存在则调用该批处理文件
if exist "%CATALINA_HOME%/bin/setclasspath.bat" goto okSetclasspath //不存在setenv.bat批处理文件时,检查是否有/bin/setclasspath.bat,有的话跳转到okSetclasspath标记处
echo Cannot find %CATALINA_HOME%/bin/setclasspath.bat //否则提示 Cannot find %CATALINA_HOME%/bin/setclasspath.bat,并且跳到结束标记处
goto end
:okSetclasspath
set BASEDIR=%CATALINA_HOME% BASEDIR环境变量在setclasspath.bat中要使用,所以先设置
call "%CATALINA_HOME%/bin/setclasspath.bat" %1 //需要先设置classpath
set CLASSPATH=%CLASSPATH%;%CATALINA_HOME%/bin/bootstrap.jar //将bootstrap.jar这个包添加到classpath上;
if not "%CATALINA_BASE%" == "" goto gotBase //CATALINA_BASE不为空则跳转到gotBase标记处
set CATALINA_BASE=%CATALINA_HOME%
:gotBase
set CATALINA_TMPDIR=%CATALINA_BASE%/temp
:gotTmpdir
echo Using CATALINA_BASE: %CATALINA_BASE%
echo Using CATALINA_HOME: %CATALINA_HOME%
echo Using CATALINA_TMPDIR: %CATALINA_TMPDIR%
echo Using JAVA_HOME: %JAVA_HOME%
set _EXECJAVA=%_RUNJAVA%
set MAINCLASS=org.apache.catalina.startup.Bootstrap
set ACTION=start
if ""%1"" == ""start"" goto doStart //如果传送给它的第一个参数为start,则跳转到doStart标记处
:doStart
if not "%OS%" == "Windows_NT" goto noTitle //检查os环境变量是不是Windows_NT
set _EXECJAVA=start "Tomcat" %_RUNJAVA% //如果是的话则设置_EXECJAVA,新启动一个名为"Tomcat"的程序
goto gotTitle //设置完后跳转到:gotTitle
:noTitle
set _EXECJAVA=start %_RUNJAVA% //启动一个没有标题的程序
:gotTitle
if not ""%1"" == ""-security"" goto execCmd //执行execCmd
//执行%_EXECJAVA%程序,后面则为传递给它的一系列可选参数
:execCmd
%_EXECJAVA% %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
//setclasspath.bat批处理文件,主要用于设置classpath环境变量,要求在运行此批处理程序之前设置了java_home变量;
if not "%JAVA_HOME%" == "" goto gotJdkHome
echo Neither the JAVA_HOME nor the JRE_HOME environment variable is define
goto exit
:gotJdkHome
if not exist "%JAVA_HOME%/bin/java.exe" goto noJavaHome
set JRE_HOME=%JAVA_HOME%
goto okJavaHome
:noJavaHome
echo The JAVA_HOME environment variable is not defined correctly
goto exit
:okJavaHome
if not "%BASEDIR%" == "" goto gotBasedir //检查BASEDIR是否已经设置
echo The BASEDIR environment variable is not defined
echo This environment variable is needed to run this program
goto exit
:gotBasedir
if exist "%BASEDIR%/bin/setclasspath.bat" goto okBasedir //判断该目录下是否有/bin/setclasspath.bat,如果没有则结束
echo The BASEDIR environment variable is not defined correctly
echo This environment variable is needed to run this program
goto exit
:okBasedir
set JAVA_ENDORSED_DIRS=%BASEDIR%/endorsed //如果有/bin/setclasspath.bat,则设置一系列的环境变量
set CLASSPATH=%JAVA_HOME%/lib/tools.jar //此处设置的CLASSPATH环境变量只有%JAVA_HOME%/lib/tools.jar,外部设置的环境变量对该classpath环境变量没用
set _RUNJAVA="%JRE_HOME%/bin/java"
1.设置CATALINA_HOME环境变量为TOMCAT安装的根目录,可以在任何地方调用startup.bat,但是调用startup.bat的本质等于调用catalina.bat,所以还需要对catalina.bat中需要的环境变量进行设置
2.通过调用setclasspath.bat设置JAVA_HOME环境变量为JDK 安装的根目录,设置catalina.bat程序运行时需要的classpath.
3.在path环境变量中加入Tomcat下bin目录,以便Tomcat安装在任何一个目录,虚拟机都能找到bin目录下的Tomcat批处理程序
4.Tomcat.exe程序与Tomcat的批处理文件可以理解为是不相干的,即是在tomcat批处理文件中设置的环境变量对Tomcat.exe程序没有影响;
5.Tomcat5.0以上的对应的djk版本需要是5.0以上版本;
6.Tomcat服务器是不会继承系统原来设置的classPath,它的classPath仅仅设置为<Catalina-home>/bin/bootstrap.jar,<Catalina-home>/bin/tools.jar两个jar包。