首页 > 代码库 > Tomcat7源码分析学习系列之一-----tomcat的真正的启动脚本catalina.bat解析

Tomcat7源码分析学习系列之一-----tomcat的真正的启动脚本catalina.bat解析

@echo off

setlocal

rem Suppress Terminate batch job on CTRL+C 
if not ""%1"" == ""run"" goto mainEntry                          rem 命令后第一个参数
if "%TEMP%" == "" goto mainEntry                           rem TEMP,系统环境变量,Windows操作系统安装后,系统会自动配置TMEP环境变量
if exist "%TEMP%\%~nx0.run" goto mainEntry                     rem %0表当前可执行文件的名称,比如catalina.bat 的名称catalina。%~nx0表示可执行文件+扩展名
echo Y>"%TEMP%\%~nx0.run"                            rem 把字母Y写入到%TEMP%\%~nx0.run文件,
if not exist "%TEMP%\%~nx0.run" goto mainEntry
echo Y>"%TEMP%\%~nx0.Y"                            rem 写入Y到 %TEMP%\catalina.bat.Y . 如果文件不存在, 则新建一个
call "%~f0" %* <"%TEMP%\%~nx0.Y"                  rem %~f0 当前可执行文件的绝对路径,%* 表示命令传递的所有参数,这一行的含义是读取%TEMP%\catalina.bat.Y的内容作为参数放到catalina.bat 命令最后
rem Use provided errorlevel
set RETVAL=%ERRORLEVEL%                         rem %ERRORLEVEL%,获取上一个命令执行后的退出码
del /Q "%TEMP%\%~nx0.Y" >NUL 2>&1               rem del /Q表示静默删除,不会给你任何提示。该行是删除 %TEMP%\catalina.bat.Y 这个文件,>nNUL 表示重定向输出到NUL中,什么到看不到。

                                   rem 2表示错误输出,&1标准输出
                                                                                                            rem 整行的含义是删除%TEMP%\catalina.bat.Y ,把错误输出的到标准输出,在输出到NUL
exit /B %RETVAL%                                                                             rem 程序退出,/B 输出退出码。即RETVAL
:mainEntry
del /Q "%TEMP%\%~nx0.run" >NUL 2>&1

rem Guess CATALINA_HOME if not defined
set "CURRENT_DIR=%cd%"
if not "%CATALINA_HOME%" == "" goto gotHome
set "CATALINA_HOME=%CURRENT_DIR%"
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
cd ..
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

rem Copy CATALINA_BASE from CATALINA_HOME if not defined
if not "%CATALINA_BASE%" == "" goto gotBase
set "CATALINA_BASE=%CATALINA_HOME%"
rem 上面从:mainEntry到此处,主要是设置本地环境变量CATALINA_HOME,和CATALINA_BASE
:gotBase

rem Ensure that any user defined CLASSPATH variables are not used on startup,
rem but allow them to be specified in setenv.bat, in rare case when it is needed.
set CLASSPATH=                               rem 设置临时环境变量CLASSPATH

rem Get standard environment variables
if not exist "%CATALINA_BASE%\bin\setenv.bat" goto checkSetenvHome
call "%CATALINA_BASE%\bin\setenv.bat"
goto setenvDone
:checkSetenvHome
if exist "%CATALINA_HOME%\bin\setenv.bat" call "%CATALINA_HOME%\bin\setenv.bat"
:setenvDone
rem 以上6行判断tomcat中bin目录是否存在setenv.bat批处理文件,存在则执行该文件。通常是不存在的


rem Get standard Java environment variables
if exist "%CATALINA_HOME%\bin\setclasspath.bat" goto okSetclasspath
echo Cannot find "%CATALINA_HOME%\bin\setclasspath.bat"
echo This file is needed to run this program
goto end
:okSetclasspath
call "%CATALINA_HOME%\bin\setclasspath.bat" %1
if errorlevel 1 goto end
rem 以上几行是判断tomcat中bin目录是否存在setclasspath.bat批处理文件,存在则执行批处理文件,不存在则提示,并退出启动tomcat。
rem setclasspath.bat主要是设置jdk相关的环境变量,因为tomcat的需要在java虚拟机上运行。
rem JAVA_HOME,jdk的目录,JRE_HOME,jre目录,JAVA_ENDORSED_DIRS = %CATALINA_HOME%\endorsed,_RUNJAVA = %JRE_HOME%\bin\java.exe,_RUNJDB = %JAVA_HOME%\bin\jdb.exe
rem Note that there are no quotes as we do not want to introduce random
rem quotes into the CLASSPATH
if "%CLASSPATH%" == "" goto emptyClasspath
set "CLASSPATH=%CLASSPATH%;"
:emptyClasspath
set "CLASSPATH=%CLASSPATH%%CATALINA_HOME%\bin\bootstrap.jar"
rem 以上几行是把tomcat中bin目录下的bootstrap.jar加入到临时环境变量CLASSPATH

if not "%CATALINA_TMPDIR%" == "" goto gotTmpdir
set "CATALINA_TMPDIR=%CATALINA_BASE%\temp"
:gotTmpdir
rem 以上3行是设置环境变量CATALINA_TMPDIR=%CATALINA_BASE%\temp,即在tomcat根目录下的temp


rem Add tomcat-juli.jar to classpath
rem tomcat-juli.jar can be over-ridden per instance
if not exist "%CATALINA_BASE%\bin\tomcat-juli.jar" goto juliClasspathHome
set "CLASSPATH=%CLASSPATH%;%CATALINA_BASE%\bin\tomcat-juli.jar"
goto juliClasspathDone
:juliClasspathHome
set "CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\bin\tomcat-juli.jar"
:juliClasspathDone
rem 以上几行是把tomcat中bin目录下的bootstrap.jar加入到临时环境变量CLASSPATH


if not "%JSSE_OPTS%" == "" goto gotJsseOpts
set JSSE_OPTS="-Djdk.tls.ephemeralDHKeySize=2048"
:gotJsseOpts
set "JAVA_OPTS=%JAVA_OPTS% %JSSE_OPTS%"

if not "%LOGGING_CONFIG%" == "" goto noJuliConfig
set LOGGING_CONFIG=-Dnop
if not exist "%CATALINA_BASE%\conf\logging.properties" goto noJuliConfig
set LOGGING_CONFIG=-Djava.util.logging.config.file="%CATALINA_BASE%\conf\logging.properties"      rem 日志配置文件文娱tomcat中的\conf目录下
:noJuliConfig

if not "%LOGGING_MANAGER%" == "" goto noJuliManager
set LOGGING_MANAGER=-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager                rem 日志管理加载类
:noJuliManager
rem 设置环境变量JAVA_OPTS LOGGING_CONFIG LOGGING_MANAGER,这些变量会作为参数追加到执行命令 java的后面,最后部分可以看到
rem ----- Execute The Requested Command ---------------------------------------

echo Using CATALINA_BASE: "%CATALINA_BASE%"
echo Using CATALINA_HOME: "%CATALINA_HOME%"
echo Using CATALINA_TMPDIR: "%CATALINA_TMPDIR%"
if ""%1"" == ""debug"" goto use_jdk
echo Using JRE_HOME: "%JRE_HOME%"
goto java_dir_displayed
:use_jdk
echo Using JAVA_HOME: "%JAVA_HOME%"
:java_dir_displayed
echo Using CLASSPATH: "%CLASSPATH%"
rem 以上是打印相关环境变量
set _EXECJAVA=%_RUNJAVA%                    rem 上面定义了_RUNJAVA = %JRE_HOME%\bin\java.exe
set MAINCLASS=org.apache.catalina.startup.Bootstrap        rem tomcat启动类
set ACTION=start rem 执行动作默认为start
set SECURITY_POLICY_FILE=
set DEBUG_OPTS=
set JPDA=
rem 设置相关环境变量,为启动tomcat最准备,以上环境变量会作为执行程序java的参数来启动tomcat
if not ""%1"" == ""jpda"" goto noJpda
set JPDA=jpda
if not "%JPDA_TRANSPORT%" == "" goto gotJpdaTransport
set JPDA_TRANSPORT=dt_socket
:gotJpdaTransport
if not "%JPDA_ADDRESS%" == "" goto gotJpdaAddress
set JPDA_ADDRESS=8000
:gotJpdaAddress
if not "%JPDA_SUSPEND%" == "" goto gotJpdaSuspend
set JPDA_SUSPEND=n
:gotJpdaSuspend
if not "%JPDA_OPTS%" == "" goto gotJpdaOpts
set JPDA_OPTS=-agentlib:jdwp=transport=%JPDA_TRANSPORT%,address=%JPDA_ADDRESS%,server=y,suspend=%JPDA_SUSPEND%
:gotJpdaOpts
shift
:noJpda
rem 以上是对jpda(java platform debug achitecture)相关参数进行设置。在执行命令catalina.bat jpda时才会进行上面的操作
if ""%1"" == ""debug"" goto doDebug
if ""%1"" == ""run"" goto doRun
if ""%1"" == ""start"" goto doStart
if ""%1"" == ""stop"" goto doStop
if ""%1"" == ""configtest"" goto doConfigTest
if ""%1"" == ""version"" goto doVersion
rem 以上是针对不同的启动策略,执行不同的脚本
echo Usage: catalina ( commands ... )
echo commands:
echo debug Start Catalina in a debugger
echo debug -security Debug Catalina with a security manager
echo jpda start Start Catalina under JPDA debugger
echo run Start Catalina in the current window
echo run -security Start in the current window with security manager
echo start Start Catalina in a separate window
echo start -security Start in a separate window with security manager
echo stop Stop Catalina
echo configtest Run a basic syntax check on server.xml
echo version What version of tomcat are you running?
goto end

:doDebug
shift
set _EXECJAVA=%_RUNJDB%
set DEBUG_OPTS=-sourcepath "%CATALINA_HOME%\..\..\java"
if not ""%1"" == ""-security"" goto execCmd
shift
echo Using Security Manager
set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
goto execCmd

:doRun
shift
if not ""%1"" == ""-security"" goto execCmd
shift
echo Using Security Manager
set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
goto execCmd

:doStart
shift
if "%TITLE%" == "" set TITLE=Tomcat
set _EXECJAVA=start "%TITLE%" %_RUNJAVA%                                    rem 如果是 startup.bat 脚本启动或者是直接用catalina.bat start命令启动的话, 会启动一个新的 cmd 窗口, 并且把 cmd 的 title 设置为 Tomcat.
if not ""%1"" == ""-security"" goto execCmd
shift
echo Using Security Manager
set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
goto execCmd

:doStop
shift
set ACTION=stop
set CATALINA_OPTS=
goto execCmd

:doConfigTest
shift
set ACTION=configtest
set CATALINA_OPTS=
goto execCmd

:doVersion
%_EXECJAVA% -classpath "%CATALINA_HOME%\lib\catalina.jar" org.apache.catalina.util.ServerInfo
goto end


:execCmd
rem Get remaining unshifted command line arguments and save them in the
set CMD_LINE_ARGS=
:setArgs
if ""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
shift
goto setArgs
:doneSetArgs
rem 以上是把执行批处理文件命令后的参数循环加载到CMD_LINE_ARGS中


rem Execute Java with the applicable properties
if not "%JPDA%" == "" goto doJpda
if not "%SECURITY_POLICY_FILE%" == "" goto doSecurity
%_EXECJAVA% %LOGGING_CONFIG% %LOGGING_MANAGER% %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%
goto end
:doSecurity
%_EXECJAVA% %LOGGING_CONFIG% %LOGGING_MANAGER% %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Djava.security.manager -Djava.security.policy=="%SECURITY_POLICY_FILE%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
goto end
:doJpda
if not "%SECURITY_POLICY_FILE%" == "" goto doSecurityJpda
%_EXECJAVA% %LOGGING_CONFIG% %LOGGING_MANAGER% %JAVA_OPTS% %JPDA_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%
goto end
:doSecurityJpda
%_EXECJAVA% %LOGGING_CONFIG% %LOGGING_MANAGER% %JAVA_OPTS% %JPDA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Djava.security.manager -Djava.security.policy=="%SECURITY_POLICY_FILE%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
goto end

:end

Tomcat7源码分析学习系列之一-----tomcat的真正的启动脚本catalina.bat解析