首页 > 代码库 > catalina.bat

catalina.bat

  startup.bat在最后调用catalina.bat,并且传递了start参数,设置了CATALINA_HOME和CURRENT_DIR俩个临时环境变量。那么catalina.bat都做了什么?

技术分享
  1 @echo off  2 rem Licensed to the Apache Software Foundation (ASF) under one or more  3 rem contributor license agreements.  See the NOTICE file distributed with  4 rem this work for additional information regarding copyright ownership.  5 rem The ASF licenses this file to You under the Apache License, Version 2.0  6 rem (the "License"); you may not use this file except in compliance with  7 rem the License.  You may obtain a copy of the License at  8 rem  9 rem     http://www.apache.org/licenses/LICENSE-2.0 10 rem 11 rem Unless required by applicable law or agreed to in writing, software 12 rem distributed under the License is distributed on an "AS IS" BASIS, 13 rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 rem See the License for the specific language governing permissions and 15 rem limitations under the License. 16  17 rem --------------------------------------------------------------------------- 18 rem Start/Stop Script for the CATALINA Server 19 rem 20 rem Environment Variable Prerequisites 21 rem 22 rem   Do not set the variables in this script. Instead put them into a script 23 rem   setenv.bat in CATALINA_BASE/bin to keep your customizations separate. 24 rem 25 rem   CATALINA_HOME   May point at your Catalina "build" directory. 26 rem 27 rem   CATALINA_BASE   (Optional) Base directory for resolving dynamic portions 28 rem                   of a Catalina installation.  If not present, resolves to 29 rem                   the same directory that CATALINA_HOME points to. 30 rem 31 rem   CATALINA_OPTS   (Optional) Java runtime options used when the "start", 32 rem                   "run" or "debug" command is executed. 33 rem                   Include here and not in JAVA_OPTS all options, that should 34 rem                   only be used by Tomcat itself, not by the stop process, 35 rem                   the version command etc. 36 rem                   Examples are heap size, GC logging, JMX ports etc. 37 rem 38 rem   CATALINA_TMPDIR (Optional) Directory path location of temporary directory 39 rem                   the JVM should use (java.io.tmpdir).  Defaults to 40 rem                   %CATALINA_BASE%\temp. 41 rem 42 rem   JAVA_HOME       Must point at your Java Development Kit installation. 43 rem                   Required to run the with the "debug" argument. 44 rem 45 rem   JRE_HOME        Must point at your Java Runtime installation. 46 rem                   Defaults to JAVA_HOME if empty. If JRE_HOME and JAVA_HOME 47 rem                   are both set, JRE_HOME is used. 48 rem 49 rem   JAVA_OPTS       (Optional) Java runtime options used when any command 50 rem                   is executed. 51 rem                   Include here and not in CATALINA_OPTS all options, that 52 rem                   should be used by Tomcat and also by the stop process, 53 rem                   the version command etc. 54 rem                   Most options should go into CATALINA_OPTS. 55 rem 56 rem   JAVA_ENDORSED_DIRS (Optional) Lists of of semi-colon separated directories 57 rem                   containing some jars in order to allow replacement of APIs 58 rem                   created outside of the JCP (i.e. DOM and SAX from W3C). 59 rem                   It can also be used to update the XML parser implementation. 60 rem                   Defaults to $CATALINA_HOME/endorsed. 61 rem 62 rem   JPDA_TRANSPORT  (Optional) JPDA transport used when the "jpda start" 63 rem                   command is executed. The default is "dt_socket". 64 rem 65 rem   JPDA_ADDRESS    (Optional) Java runtime options used when the "jpda start" 66 rem                   command is executed. The default is 8000. 67 rem 68 rem   JPDA_SUSPEND    (Optional) Java runtime options used when the "jpda start" 69 rem                   command is executed. Specifies whether JVM should suspend 70 rem                   execution immediately after startup. Default is "n". 71 rem 72 rem   JPDA_OPTS       (Optional) Java runtime options used when the "jpda start" 73 rem                   command is executed. If used, JPDA_TRANSPORT, JPDA_ADDRESS, 74 rem                   and JPDA_SUSPEND are ignored. Thus, all required jpda 75 rem                   options MUST be specified. The default is: 76 rem 77 rem                   -agentlib:jdwp=transport=%JPDA_TRANSPORT%, 78 rem                       address=%JPDA_ADDRESS%,server=y,suspend=%JPDA_SUSPEND% 79 rem 80 rem   LOGGING_CONFIG  (Optional) Override Tomcats logging config file 81 rem                   Example (all one line) 82 rem                   set LOGGING_CONFIG="-Djava.util.logging.config.file=%CATALINA_BASE%\conf\logging.properties" 83 rem 84 rem   LOGGING_MANAGER (Optional) Override Tomcats logging manager 85 rem                   Example (all one line) 86 rem                   set LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager" 87 rem 88 rem   TITLE           (Optional) Specify the title of Tomcat window. The default 89 rem                   TITLE is Tomcat if its not specified. 90 rem                   Example (all one line) 91 rem                   set TITLE=Tomcat.Cluster#1.Server#1 [%DATE% %TIME%] 92 rem --------------------------------------------------------------------------- 93  94 setlocal 95  96 rem Suppress Terminate batch job on CTRL+C 97 if not ""%1"" == ""run"" goto mainEntry 98 if "%TEMP%" == "" goto mainEntry 99 if exist "%TEMP%\%~nx0.run" goto mainEntry100 echo Y>"%TEMP%\%~nx0.run"101 if not exist "%TEMP%\%~nx0.run" goto mainEntry102 echo Y>"%TEMP%\%~nx0.Y"103 call "%~f0" %* <"%TEMP%\%~nx0.Y"104 rem Use provided errorlevel105 set RETVAL=%ERRORLEVEL%106 del /Q "%TEMP%\%~nx0.Y" >NUL 2>&1107 exit /B %RETVAL%108 :mainEntry109 del /Q "%TEMP%\%~nx0.run" >NUL 2>&1110 111 rem Guess CATALINA_HOME if not defined112 set "CURRENT_DIR=%cd%"113 if not "%CATALINA_HOME%" == "" goto gotHome114 set "CATALINA_HOME=%CURRENT_DIR%"115 if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome116 cd ..117 set "CATALINA_HOME=%cd%"118 cd "%CURRENT_DIR%"119 :gotHome120 121 if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome122 echo The CATALINA_HOME environment variable is not defined correctly123 echo This environment variable is needed to run this program124 goto end125 :okHome126 127 rem Copy CATALINA_BASE from CATALINA_HOME if not defined128 if not "%CATALINA_BASE%" == "" goto gotBase129 set "CATALINA_BASE=%CATALINA_HOME%"130 :gotBase131 132 rem Ensure that any user defined CLASSPATH variables are not used on startup,133 rem but allow them to be specified in setenv.bat, in rare case when it is needed.134 set CLASSPATH=135 136 rem Get standard environment variables137 if not exist "%CATALINA_BASE%\bin\setenv.bat" goto checkSetenvHome138 call "%CATALINA_BASE%\bin\setenv.bat"139 goto setenvDone140 :checkSetenvHome141 if exist "%CATALINA_HOME%\bin\setenv.bat" call "%CATALINA_HOME%\bin\setenv.bat"142 :setenvDone143 144 rem Get standard Java environment variables145 if exist "%CATALINA_HOME%\bin\setclasspath.bat" goto okSetclasspath146 echo Cannot find "%CATALINA_HOME%\bin\setclasspath.bat"147 echo This file is needed to run this program148 goto end149 :okSetclasspath150 call "%CATALINA_HOME%\bin\setclasspath.bat" %1151 if errorlevel 1 goto end152 153 rem Add on extra jar file to CLASSPATH154 rem Note that there are no quotes as we do not want to introduce random155 rem quotes into the CLASSPATH156 if "%CLASSPATH%" == "" goto emptyClasspath157 set "CLASSPATH=%CLASSPATH%;"158 :emptyClasspath159 set "CLASSPATH=%CLASSPATH%%CATALINA_HOME%\bin\bootstrap.jar"160 161 if not "%CATALINA_TMPDIR%" == "" goto gotTmpdir162 set "CATALINA_TMPDIR=%CATALINA_BASE%\temp"163 :gotTmpdir164 165 rem Add tomcat-juli.jar to classpath166 rem tomcat-juli.jar can be over-ridden per instance167 if not exist "%CATALINA_BASE%\bin\tomcat-juli.jar" goto juliClasspathHome168 set "CLASSPATH=%CLASSPATH%;%CATALINA_BASE%\bin\tomcat-juli.jar"169 goto juliClasspathDone170 :juliClasspathHome171 set "CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\bin\tomcat-juli.jar"172 :juliClasspathDone173 174 if not "%LOGGING_CONFIG%" == "" goto noJuliConfig175 set LOGGING_CONFIG=-Dnop176 if not exist "%CATALINA_BASE%\conf\logging.properties" goto noJuliConfig177 set LOGGING_CONFIG=-Djava.util.logging.config.file="%CATALINA_BASE%\conf\logging.properties"178 :noJuliConfig179 set "JAVA_OPTS=%JAVA_OPTS% %LOGGING_CONFIG%"180 181 if not "%LOGGING_MANAGER%" == "" goto noJuliManager182 set LOGGING_MANAGER=-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager183 :noJuliManager184 set "JAVA_OPTS=%JAVA_OPTS% %LOGGING_MANAGER%"185 186 rem ----- Execute The Requested Command ---------------------------------------187 188 echo Using CATALINA_BASE:   "%CATALINA_BASE%"189 echo Using CATALINA_HOME:   "%CATALINA_HOME%"190 echo Using CATALINA_TMPDIR: "%CATALINA_TMPDIR%"191 if ""%1"" == ""debug"" goto use_jdk192 echo Using JRE_HOME:        "%JRE_HOME%"193 goto java_dir_displayed194 :use_jdk195 echo Using JAVA_HOME:       "%JAVA_HOME%"196 :java_dir_displayed197 echo Using CLASSPATH:       "%CLASSPATH%"198 199 set _EXECJAVA=%_RUNJAVA%200 set MAINCLASS=org.apache.catalina.startup.Bootstrap201 set ACTION=start202 set SECURITY_POLICY_FILE=203 set DEBUG_OPTS=204 set JPDA=205 206 if not ""%1"" == ""jpda"" goto noJpda207 set JPDA=jpda208 if not "%JPDA_TRANSPORT%" == "" goto gotJpdaTransport209 set JPDA_TRANSPORT=dt_socket210 :gotJpdaTransport211 if not "%JPDA_ADDRESS%" == "" goto gotJpdaAddress212 set JPDA_ADDRESS=8000213 :gotJpdaAddress214 if not "%JPDA_SUSPEND%" == "" goto gotJpdaSuspend215 set JPDA_SUSPEND=n216 :gotJpdaSuspend217 if not "%JPDA_OPTS%" == "" goto gotJpdaOpts218 set JPDA_OPTS=-agentlib:jdwp=transport=%JPDA_TRANSPORT%,address=%JPDA_ADDRESS%,server=y,suspend=%JPDA_SUSPEND%219 :gotJpdaOpts220 shift221 :noJpda222 223 if ""%1"" == ""debug"" goto doDebug224 if ""%1"" == ""run"" goto doRun225 if ""%1"" == ""start"" goto doStart226 if ""%1"" == ""stop"" goto doStop227 if ""%1"" == ""configtest"" goto doConfigTest228 if ""%1"" == ""version"" goto doVersion229 230 echo Usage:  catalina ( commands ... )231 echo commands:232 echo   debug             Start Catalina in a debugger233 echo   debug -security   Debug Catalina with a security manager234 echo   jpda start        Start Catalina under JPDA debugger235 echo   run               Start Catalina in the current window236 echo   run -security     Start in the current window with security manager237 echo   start             Start Catalina in a separate window238 echo   start -security   Start in a separate window with security manager239 echo   stop              Stop Catalina240 echo   configtest        Run a basic syntax check on server.xml241 echo   version           What version of tomcat are you running?242 goto end243 244 :doDebug245 shift246 set _EXECJAVA=%_RUNJDB%247 set DEBUG_OPTS=-sourcepath "%CATALINA_HOME%\..\..\java"248 if not ""%1"" == ""-security"" goto execCmd249 shift250 echo Using Security Manager251 set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"252 goto execCmd253 254 :doRun255 shift256 if not ""%1"" == ""-security"" goto execCmd257 shift258 echo Using Security Manager259 set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"260 goto execCmd261 262 :doStart263 shift264 if "%TITLE%" == "" set TITLE=Tomcat265 set _EXECJAVA=start "%TITLE%" %_RUNJAVA%266 if not ""%1"" == ""-security"" goto execCmd267 shift268 echo Using Security Manager269 set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"270 goto execCmd271 272 :doStop273 shift274 set ACTION=stop275 set CATALINA_OPTS=276 goto execCmd277 278 :doConfigTest279 shift280 set ACTION=configtest281 set CATALINA_OPTS=282 goto execCmd283 284 :doVersion285 %_EXECJAVA% -classpath "%CATALINA_HOME%\lib\catalina.jar" org.apache.catalina.util.ServerInfo286 goto end287 288 289 :execCmd290 rem Get remaining unshifted command line arguments and save them in the291 set CMD_LINE_ARGS=292 :setArgs293 if ""%1""=="""" goto doneSetArgs294 set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1295 shift296 goto setArgs297 :doneSetArgs298 299 rem Execute Java with the applicable properties300 if not "%JPDA%" == "" goto doJpda301 if not "%SECURITY_POLICY_FILE%" == "" goto doSecurity302 %_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%303 goto end304 :doSecurity305 %_EXECJAVA% %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%306 goto end307 :doJpda308 if not "%SECURITY_POLICY_FILE%" == "" goto doSecurityJpda309 %_EXECJAVA% %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%310 goto end311 :doSecurityJpda312 %_EXECJAVA% %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%313 goto end314 315 :end
View Code

  line    1  @echo off  关闭批处理输出

  line  94  setlocal  开始设置临时环境变量

  line  97  if not ""%1"" == ""run"" goto mainEntry  如果第一个参数不是run的话,goto mainEntry节点(第一个参数默认start,所以会直接goto mianEntry节点);否则继续。

  line  98-108  如果第一个参数是run的话才会被执行,其中加黑的103,105,107并未理解。

  line  98  if "%TEMP%" == "" goto mainEntry  如果没有TEMP环境变量的话,goto mianEntry节点;否则继续。我试了一下echo %TEMP%,得到的是*\AppData\Local\Temp,所以这个和startup.bat中的%OS%一样是存在的,继续line99

  line  99  if exist "%TEMP%\%~nx0.run" goto mainEntry  如果在%TEMP% 目录下有 %~nx0.run,goto mainEntry,但是%~nx0.run是什么呢?搜索了一下,%0就是本BAT的绝对路径,%~ 扩充删除任何引号,%~nx ::n文件名 ::x扩展名。试着写了个test.bat,内容是echo "%TEMP%\%~nx0.run",执行结果:*\AppData\Local\Temp\test.bat.run,所以这一行是如果在%TEMP%下有catalina.bat.run,goto mainEntry节点,我查了下对应目录下并没有catalina.bat.run,继续line100。

  line100  echo Y>"%TEMP%\%~nx0.run"  创建catalina.bat.run并写入Y。

  line101  if not exist "%TEMP%\%~nx0.run" goto mainEntry  如果%TEMP%下仍没有catalina.bat.run,goto mainEntry,但line100创建并写入了Y,继续下一行。

  line102  echo Y>"%TEMP%\%~nx0.Y"  创建catalina.bat.Y并写入Y。

  line103  call "%~f0" %* <"%TEMP%\%~nx0.Y"  试了下echo "%~f0",执行结果:*\Desktop\test.bat。%*是命令行传的所有参数,"%TEMP%\%~nx0.Y"是line102创建的文件内容为Y。所以line103做的事情就是catalina.bat自己调用自己,参数为Y。为什么呢????

  line105  set RETVAL=%ERRORLEVEL%  试了下echo %ERRORLEVEL%,执行结果:0,但是搜索了一下%ERRORLEVEL%是返回的错误码,0是成功,其他是失败。

  line106  del /Q "%TEMP%\%~nx0.Y" >NUL 2>&1  del /Q是安静模式删除,不要求确认。NUL是DOS下的一个虚拟设备,相当于一个文件(输入时立即结束,输出时仅作模拟)。2>&1,2是值错误输出,&1是标准输出,意思就是将错误输出重定向到标准输出中。>NUL 2>&1,就是将错误输入重定向到标准输出,重定向到NUL中。这行作用就是安静模式删除*\AppData\Local\Temp\test.bat.Y。

  DEL命令:

DEL [/P] [/F] [/S] [/Q] [/A[[:]attributes]] names
/P 删除每一个文件之前提示确认。 /F 强制删除只读文件。 /S 从所有子目录删除指定文件。 /Q 安静模式。删除全局通配符时,不要求确认。 /A 根据属性选择要删除的文件。

  line107  exit /B %RETVAL%  ,在cmd中输入了一下help exit,得到如下结果: 

退出 CMD.EXE 程序(命令解释器)或当前批处理脚本。EXIT [/B] [exitCode]  /B          指定要退出当前批处理脚本而不是 CMD.EXE。如果从一个              批处理脚本外执行,则会退出 CMD.EXE  exitCode    指定一个数字号码。如果指定了 /B,将 ERRORLEVEL              设成那个数字。如果退出 CMD.EXE,则用那个数字设置              过程退出代码。

  line108  :mainEntry  :mainEntry节点

  line109  del /Q "%TEMP%\%~nx0.run" >NUL 2>&1  安静模式删除*\AppData\Local\Temp\test.bat.run。

  为了搞明白103~107,我在catalina.bat中加入了几行输出,把catalina.bat.run的创建和删除注释掉了,如下:

 1 if not ""%1"" == ""run"" goto mainEntry 2 if "%TEMP%" == "" goto mainEntry 3 if exist "%TEMP%\%~nx0.run" goto mainEntry 4 rem echo Y>"%TEMP%\%~nx0.run" 5 rem if not exist "%TEMP%\%~nx0.run" goto mainEntry 6 echo Y>"%TEMP%\%~nx0.Y" 7 echo ======================================1 8 call "%~f0" %* <"%TEMP%\%~nx0.Y" 9 echo ======================================210 rem Use provided errorlevel11 set RETVAL=%ERRORLEVEL%12 echo ====================================="%ERRORLEVEL%"13 echo ====================================="%RETVAL%14 del /Q "%TEMP%\%~nx0.Y" >NUL 2>&115 exit /B %RETVAL%16 :mainEntry17 rem del /Q "%TEMP%\%~nx0.run" >NUL 2>&1

  我直接启动catalina.bat run,用run做第一个参数,因为如果%TEMP%下没有catalina.bat.run的话会创建,然后我注释了line5,这样不会goto mainEntry节点,而是继续执行。line7会有输出,而后就没有了。line8 ,catalina.bat使用Y参数自己调用自己,然后走到line1,因为参数不是run而是Y,goto mainEntry节点。之后我将catalina.bat.Y修改为run试了下,会停留在line7一直输出=====1。那么line9~line15什么时候会执行呢?

我启动catalina.bat run,然后运行shutdown.bat,line9输出,%ERRORLEVEL%是0,catalina.bat.Y被删除,catalina.bat被停掉。简单扫了眼shutdown.bat,这个batch file 在最后call catalina.bat stop,即停掉了catalina.bat。

  line111~125  和startup.bat做的事情差不多,如果是从startup.bat启动的话,CURRENT_DIR和CATALINA_HOME俩个变量在startup里已经设置过,依然有效。

技术分享
rem Guess CATALINA_HOME if not definedset "CURRENT_DIR=%cd%"if not "%CATALINA_HOME%" == "" goto gotHomeset "CATALINA_HOME=%CURRENT_DIR%"if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHomecd ..set "CATALINA_HOME=%cd%"cd "%CURRENT_DIR%":gotHomeif exist "%CATALINA_HOME%\bin\catalina.bat" goto okHomeecho The CATALINA_HOME environment variable is not defined correctlyecho This environment variable is needed to run this programgoto end:okHome
111~125

  line127~130  如果没有CATALINA_BASE这个环境变量的话,把CATALINA_HOME的值给它(CATALINA_HOME这个环境变量我都没有提前配置,CATALINA_BASE肯定也不会配置)。

技术分享
rem Copy CATALINA_BASE from CATALINA_HOME if not definedif not "%CATALINA_BASE%" == "" goto gotBaseset "CATALINA_BASE=%CATALINA_HOME%":gotBase
127~130

  line132~134  set CLASSPATH=  设置CLASSPATH临时环境变量,注释为:Ensure that any user defined CLASSPATH variables are not used on startup, but allow them to be specified in setenv.bat, in rare case when it is needed. 大意是:确认在启动时不使用任何用户定义的CLASSPATH,但是在允许在setenv.bat里指定它们(CLASSPATH variables)。搜索了一下setenv.bat发现可以指定另外的JAVA_HOME和JAVA_OPTS等等环境变量。

  line136~142  如果用户在%CATALINA_HOME%\bin或者%CATALINA_BASE%\bin下建了setenv.bat就调用setenv.bat。

技术分享
rem Get standard environment variablesif not exist "%CATALINA_BASE%\bin\setenv.bat" goto checkSetenvHomecall "%CATALINA_BASE%\bin\setenv.bat"goto setenvDone:checkSetenvHomeif exist "%CATALINA_HOME%\bin\setenv.bat" call "%CATALINA_HOME%\bin\setenv.bat":setenvDone
136~142

  line144~151  调用%CATALINA_HOME%\bin\setclasspath.bat,获取java相关环境变量。

技术分享
rem Get standard Java environment variablesif exist "%CATALINA_HOME%\bin\setclasspath.bat" goto okSetclasspathecho Cannot find "%CATALINA_HOME%\bin\setclasspath.bat"echo This file is needed to run this programgoto end:okSetclasspathcall "%CATALINA_HOME%\bin\setclasspath.bat" %1if errorlevel 1 goto end
144~151

  line145  if exist "%CATALINA_HOME%\bin\setclasspath.bat" goto okSetclasspath  如果文件存在,goto okSetclasspath节点,否则执行line146~147后goto end节点退出本次批处理。

  line150  call "%CATALINA_HOME%\bin\setclasspath.bat" %1  调用%CATALINA_HOME%\bin\setclasspath.bat,参数是catalina.bat的第一个参数(即从startup.bat拿到的start)。

技术分享
 1 @echo off 2 rem Licensed to the Apache Software Foundation (ASF) under one or more 3 rem contributor license agreements.  See the NOTICE file distributed with 4 rem this work for additional information regarding copyright ownership. 5 rem The ASF licenses this file to You under the Apache License, Version 2.0 6 rem (the "License"); you may not use this file except in compliance with 7 rem the License.  You may obtain a copy of the License at 8 rem 9 rem     http://www.apache.org/licenses/LICENSE-2.010 rem11 rem Unless required by applicable law or agreed to in writing, software12 rem distributed under the License is distributed on an "AS IS" BASIS,13 rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14 rem See the License for the specific language governing permissions and15 rem limitations under the License.16 17 rem ---------------------------------------------------------------------------18 rem Set JAVA_HOME or JRE_HOME if not already set, ensure any provided settings19 rem are valid and consistent with the selected start-up options and set up the20 rem endorsed directory.21 rem ---------------------------------------------------------------------------22 23 rem Make sure prerequisite environment variables are set24 25 rem In debug mode we need a real JDK (JAVA_HOME)26 if ""%1"" == ""debug"" goto needJavaHome27 28 rem Otherwise either JRE or JDK are fine29 if not "%JRE_HOME%" == "" goto gotJreHome30 if not "%JAVA_HOME%" == "" goto gotJavaHome31 echo Neither the JAVA_HOME nor the JRE_HOME environment variable is defined32 echo At least one of these environment variable is needed to run this program33 goto exit34 35 :needJavaHome36 rem Check if we have a usable JDK37 if "%JAVA_HOME%" == "" goto noJavaHome38 if not exist "%JAVA_HOME%\bin\java.exe" goto noJavaHome39 if not exist "%JAVA_HOME%\bin\javaw.exe" goto noJavaHome40 if not exist "%JAVA_HOME%\bin\jdb.exe" goto noJavaHome41 if not exist "%JAVA_HOME%\bin\javac.exe" goto noJavaHome42 set "JRE_HOME=%JAVA_HOME%"43 goto okJava44 45 :noJavaHome46 echo The JAVA_HOME environment variable is not defined correctly.47 echo It is needed to run this program in debug mode.48 echo NB: JAVA_HOME should point to a JDK not a JRE.49 goto exit50 51 :gotJavaHome52 rem No JRE given, use JAVA_HOME as JRE_HOME53 set "JRE_HOME=%JAVA_HOME%"54 55 :gotJreHome56 rem Check if we have a usable JRE57 if not exist "%JRE_HOME%\bin\java.exe" goto noJreHome58 if not exist "%JRE_HOME%\bin\javaw.exe" goto noJreHome59 goto okJava60 61 :noJreHome62 rem Needed at least a JRE63 echo The JRE_HOME environment variable is not defined correctly64 echo This environment variable is needed to run this program65 goto exit66 67 :okJava68 rem Dont override the endorsed dir if the user has set it previously69 if not "%JAVA_ENDORSED_DIRS%" == "" goto gotEndorseddir70 rem Set the default -Djava.endorsed.dirs argument71 set "JAVA_ENDORSED_DIRS=%CATALINA_HOME%\endorsed"72 :gotEndorseddir73 74 rem Dont override _RUNJAVA if the user has set it previously75 if not "%_RUNJAVA%" == "" goto gotRunJava76 rem Set standard command for invoking Java.77 rem Also note the quoting as JRE_HOME may contain spaces.78 set _RUNJAVA="%JRE_HOME%\bin\java.exe"79 :gotRunJava80 81 rem Dont override _RUNJDB if the user has set it previously82 rem Also note the quoting as JAVA_HOME may contain spaces.83 if not "%_RUNJDB%" == "" goto gotRunJdb84 set _RUNJDB="%JAVA_HOME%\bin\jdb.exe"85 :gotRunJdb86 87 goto end88 89 :exit90 exit /b 191 92 :end93 exit /b 0
setclasspath.bat

  setclasspath.bat比较简单。In debug mode we need a real JDK (JAVA_HOME),如果没有JRE_HOME和JAVA_HOME,goto exit等等,设置了%JAVA_ENDORSED_DIRS%、%_RUNJAVA%和%_RUNJDB%,执行完毕返回结果0或1。

  line151  if errorlevel 1 goto end  如果setclasspath.bat的结果是1,goto end节点,退出本次批处理。

  line153~159  如果有%CLASSPATH%的话,CLASSPATH=";" + %CATALINA_HOME%\bin\bootstrap.jar;否则CLASSPATH=%CATALINA_HOME%\bin\bootstrap.jar。

技术分享
rem Add on extra jar file to CLASSPATHrem Note that there are no quotes as we do not want to introduce randomrem quotes into the CLASSPATHif "%CLASSPATH%" == "" goto emptyClasspathset "CLASSPATH=%CLASSPATH%;":emptyClasspathset "CLASSPATH=%CLASSPATH%%CATALINA_HOME%\bin\bootstrap.jar"
153~159

  line161~163  如果没有%CATALINA_TMPDIR%环境变量的话,设置%CATALINA_TMPDIR%=%CATALINA_BASE%\temp。

技术分享
if not "%CATALINA_TMPDIR%" == "" goto gotTmpdirset "CATALINA_TMPDIR=%CATALINA_BASE%\temp":gotTmpdir
161~163

  line165~172  把tomcat-juli.jar写进CLASSPATH里。

技术分享
rem Add tomcat-juli.jar to classpathrem tomcat-juli.jar can be over-ridden per instanceif not exist "%CATALINA_BASE%\bin\tomcat-juli.jar" goto juliClasspathHomeset "CLASSPATH=%CLASSPATH%;%CATALINA_BASE%\bin\tomcat-juli.jar"goto juliClasspathDone:juliClasspathHomeset "CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\bin\tomcat-juli.jar":juliClasspathDone
165~172

  line174~184  对日志的处理,给%JAVA_OPTS%加上了%LOGGING_CONFIG% %LOGGING_MANAGER%。

技术分享
if not "%LOGGING_CONFIG%" == "" goto noJuliConfigset LOGGING_CONFIG=-Dnopif not exist "%CATALINA_BASE%\conf\logging.properties" goto noJuliConfigset LOGGING_CONFIG=-Djava.util.logging.config.file="%CATALINA_BASE%\conf\logging.properties":noJuliConfigset "JAVA_OPTS=%JAVA_OPTS% %LOGGING_CONFIG%"if not "%LOGGING_MANAGER%" == "" goto noJuliManagerset LOGGING_MANAGER=-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager:noJuliManagerset "JAVA_OPTS=%JAVA_OPTS% %LOGGING_MANAGER%"
174~184

  line186  rem ----- Execute The Requested Command   执行请求。

  line188~190  输出了CATALINA_BASE、CATALINA_HOME和CATALINA_TMPDIR,从上边可以看出,如果没有设置CATALINA_BASE环境变量的话,CATALINA_BASE=CATALINA_HOME,而CATALINA_TMPDIR=在CATALINA_BASE\temp,而CATALINA_HOME就是catalina.bat的目录(startup.bat一般和catalina.bat在一起)。

  line190~197  如果第一个参数是debug,输出%JAVA_HOME%;否则输出%JRE_HOME%,输出拼接的%CLASSPATH%。

技术分享
echo Using CATALINA_BASE:   "%CATALINA_BASE%"echo Using CATALINA_HOME:   "%CATALINA_HOME%"echo Using CATALINA_TMPDIR: "%CATALINA_TMPDIR%"if ""%1"" == ""debug"" goto use_jdkecho Using JRE_HOME:        "%JRE_HOME%"goto java_dir_displayed:use_jdkecho Using JAVA_HOME:       "%JAVA_HOME%":java_dir_displayedecho Using CLASSPATH:       "%CLASSPATH%"
188~197

  line199~204  设置了6个变量。_EXECJAVA=%_RUNJAVA%,%_RUNJAVA%这个环境变量是在setclasspath.bat中设置的,值为%JRE_HOME%\bin\java.exe;MAINCLASS=org.apache.catalina.startup.Bootstrap,这个类在%CATALINA_HOME%\bin\bootstarp.jar;ACTION=start;其他3个变量没有赋值。

技术分享
set _EXECJAVA=%_RUNJAVA%set MAINCLASS=org.apache.catalina.startup.Bootstrapset ACTION=startset SECURITY_POLICY_FILE=set DEBUG_OPTS=set JPDA=
199~204

  line206~221  是对jdpa的一些操作,搜了一下jdpa的简介,是调试java的api。

技术分享
if not ""%1"" == ""jpda"" goto noJpdaset JPDA=jpdaif not "%JPDA_TRANSPORT%" == "" goto gotJpdaTransportset JPDA_TRANSPORT=dt_socket:gotJpdaTransportif not "%JPDA_ADDRESS%" == "" goto gotJpdaAddressset JPDA_ADDRESS=8000:gotJpdaAddressif not "%JPDA_SUSPEND%" == "" goto gotJpdaSuspendset JPDA_SUSPEND=n:gotJpdaSuspendif not "%JPDA_OPTS%" == "" goto gotJpdaOptsset JPDA_OPTS=-agentlib:jdwp=transport=%JPDA_TRANSPORT%,address=%JPDA_ADDRESS%,server=y,suspend=%JPDA_SUSPEND%:gotJpdaOptsshift:noJpda
206~221

   jdpa简介:

Java平台调试器架构(英语:Java Platform Debugger Architecture,JPDA)是一组用于调试Java代码的API。Java调试器接口(Java Debugger Interface,JDI)——定义了一个高层次Java接口,开发人员可以利用JDI轻松编写远程调试工具。Java虚拟机工具接口(Java Virtual Machine Tools Interface,JVMTI)——定义了一个原生(native)接口,可以对运行在Java虚拟机的应用程序检查状态、控制运行。Java虚拟机调试接口(JVMDI)——JVMDI在J2SE 5.0中被JVMTI取代,并在Java SE 6中被移除。Java调试线协议(JDWP)——定义了调试对象(一个 Java 应用程序)和调试器进程之间的通信协议。

  line223~242  判断catalina.bat的第一个参数,goto 对应的节点。如果第一个参数不是debug、run、start、stop、configtest、version,goto end,本次批处理结束。

技术分享
if ""%1"" == ""debug"" goto doDebugif ""%1"" == ""run"" goto doRunif ""%1"" == ""start"" goto doStartif ""%1"" == ""stop"" goto doStopif ""%1"" == ""configtest"" goto doConfigTestif ""%1"" == ""version"" goto doVersionecho Usage:  catalina ( commands ... )echo commands:echo   debug             Start Catalina in a debuggerecho   debug -security   Debug Catalina with a security managerecho   jpda start        Start Catalina under JPDA debuggerecho   run               Start Catalina in the current windowecho   run -security     Start in the current window with security managerecho   start             Start Catalina in a separate windowecho   start -security   Start in a separate window with security managerecho   stop              Stop Catalinaecho   configtest        Run a basic syntax check on server.xmlecho   version           What version of tomcat are you running?goto end
223~242

  line244~252  doDebug节点

  line245  shift  表示批处理文件中替换参数左移一个位置,后面的替换参数陆续填补上去。

  line246  set _EXECJAVA=%_RUNJDB%  这个变量在line199已经设置过,%_RUNJAVA%这个环境变量是在setclasspath.bat中设置的,值为%JRE_HOME%\bin\java.exe。

  line247  set DEBUG_OPTS=-sourcepath "%CATALINA_HOME%\..\..\java"  

  line248  if not ""%1"" == ""-security"" goto execCmd  因为245有一个shift命令,所以此处的%1是catalina.bat启动时的第二个参数。如果%1不是-security的话,goto execCmd节点;否则继续。

  line249  shift  参数再次左移

  line251  set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"  这个SECURITY_POLICY_FILE赋值。

技术分享
:doDebugshiftset _EXECJAVA=%_RUNJDB%set DEBUG_OPTS=-sourcepath "%CATALINA_HOME%\..\..\java"if not ""%1"" == ""-security"" goto execCmdshiftecho Using Security Managerset "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"goto execCmd
debug节点244~252

  line254~260  doRun节点,如果启动时第二个参数是-security,set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy";否则直接goto execCmd节点。

技术分享
:doRunshiftif not ""%1"" == ""-security"" goto execCmdshiftecho Using Security Managerset "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"goto execCmd
dorun节点254~260

  line262~270  doStart节点。设置TITLE=TOMCAT,设置_EXECJAVA=start "TOMCAT" %_RUNJAVA%,这样cmd窗口的title会变为“TOMCAT”。如果启动时第二个参数是-security,set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy";否则直接goto execCmd节点。

技术分享
:doStartshiftif "%TITLE%" == "" set TITLE=Tomcatset _EXECJAVA=start "%TITLE%" %_RUNJAVA%if not ""%1"" == ""-security"" goto execCmdshiftecho Using Security Managerset "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"goto execCmd
dostart262~270

  line272~276  doStop节点,改变ACTION=stop,CATALINA_OPTS=,goto execCmd节点。

技术分享
:doStopshiftset ACTION=stopset CATALINA_OPTS=goto execCmd
dostop272~270

  line278~282  doConfigTest节点,改变ACTION=configtest,CATALINA_OPTS=,goto execCmd节点。

技术分享
:doConfigTestshiftset ACTION=configtestset CATALINA_OPTS=goto execCmd
doConfigTest278~282

  line284~286  doVersion节点,执行line285后goto end节点,本次批处理结束。

  line285  %_EXECJAVA% -classpath "%CATALINA_HOME%\lib\catalina.jar" org.apache.catalina.util.ServerInfo  等同于java -classpath "*\catalina.jar" org.apache.catalina.util.ServerInfo

技术分享
:doVersion%_EXECJAVA% -classpath "%CATALINA_HOME%\lib\catalina.jar" org.apache.catalina.util.ServerInfogoto end
doVersion284~286

  反编译后的ServerInfo(片段):

...
public static void main(String[] args) { System.out.println("Server version: " + getServerInfo()); System.out.println("Server built: " + getServerBuilt()); System.out.println("Server number: " + getServerNumber()); System.out.println("OS Name: " + System.getProperty("os.name")); System.out.println("OS Version: " + System.getProperty("os.version")); System.out.println("Architecture: " + System.getProperty("os.arch")); System.out.println("JVM Version: " + System.getProperty("java.runtime.version")); System.out.println("JVM Vendor: " + System.getProperty("java.vm.vendor")); }
 /**
  * 从
ServerInfo.properties读取服务器信息,如果读取失败会有默认返回。
  */
static {
Properties props = new Properties();
    InputStream is = null;    try {      is = ServerInfo.class.getResourceAsStream("/org/apache/catalina/util/ServerInfo.properties"); // 这个配置文件里只有server.info, server.built, server.number3个属性      props.load(is);      serverInfo = props.getProperty("server.info");      serverBuilt = props.getProperty("server.built");      serverNumber = props.getProperty("server.number");    } catch (Throwable t) {      ExceptionUtils.handleThrowable(t);    } finally {      if (is != null)        try {          is.close();        }        catch (IOException e) {        }    }    if (serverInfo == null)      serverInfo = "Apache Tomcat 7.0.x-dev";    if (serverBuilt == null)      serverBuilt = "unknown";    if (serverNumber == null)      serverNumber = "7.0.x";  }
...

  line289~297  execCmd节点,拼接剩余的参数给CMD_LINE_ARGS,和startup.bat中一样。

  line299~313  以4种方式之一启动bootstarp.jar,启动后都goto end节点,本次批处理结束,设置的临时环境变量失效。

  line302  %_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%  这种启动方式是没有jdpa和SECURITY_POLICY_FILE的启动。这种模式的%_EXECJAVA%=start "Tomcat" "%JRE_HOME%\bin\java.exe";%JAVA_OPTS%是line174~184对日志的处理;CATALINA_OPTS="";%DEBUG_OPTS%="";%JAVA_ENDORSED_DIRS%在setclasspath.bat设置为%CATALINA_HOME%\endorsed;%CLASSPATH%是拼接了bootstrap.jar和tomcat-juli.jar俩个jar包的绝对路径;%MAINCLASS%="org.apache.catalina.startup.Bootstrap"是bootstrap.jar的要启动的main方法所在类;%CMD_LINE_ARGS%是catalina.bat启动时传入参数拼接而成(从第三个参数开始拼接,因为doStart节点的line263和line267有2处shift),一般为空;%ACTION%=start。简言,就是启动了bootstrap.jar,main方法是org.apache.catalina.startup.Bootstrap,参数肯定会有一个start(如果有其他参数,这个会在最后)。

  line305  %_EXECJAVA% %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%  这种启动方式是没有jdpa但是有SECURITY_POLICY_FILE的启动。比line302多了-Djava.security.policy=="%SECURITY_POLICY_FILE%",%SECURITY_POLICY_FILE%=%CATALINA_BASE%\conf\catalina.policy,catalina.policy是大约TOMCAT的权限控制之类。

  line309  %_EXECJAVA% %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%  这种启动方式有jdpa无SECURITY_POLICY_FILE,其中%JPDA_OPTS%在line206~221设置。

  line312  %_EXECJAVA% %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%  这种启动方式有jpda和SECURITY_POLICY_FILE

技术分享
rem Execute Java with the applicable propertiesif not "%JPDA%" == "" goto doJpdaif not "%SECURITY_POLICY_FILE%" == "" goto doSecurity%_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%goto end:doSecurity%_EXECJAVA% %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:doJpdaif not "%SECURITY_POLICY_FILE%" == "" goto doSecurityJpda%_EXECJAVA% %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% %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
299~313

  line315  :end  end节点。

  这个批处理比startup.bat复杂的多,tomcat允许我们在%CATALINA_BASE%\bin\setenv.bat设置环境变量,读取%CATALINA_HOME%\bin\setclasspath.bat获取java相关的环境变量,拼接classpath,设置日志相关,设置JPDA_OPTS相关,%CATALINA_BASE%\conf\catalina.policy的权限控制,如果要查看版本信息,会启动%CATALINA_HOME%\lib\catalina.jar下的org.apache.catalina.util.ServerInfo等等最终,也是一般情况(startup.bat启动catalina.bat)会在最后启动bootstrap.jar的org.apache.catalina.startup.Bootstrap类,参数为start。

catalina.bat