标签:
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 Tomcat‘s 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 Tomcat‘s 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 it‘s 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 mainEntry 100 echo Y>"%TEMP%\%~nx0.run" 101 if not exist "%TEMP%\%~nx0.run" goto mainEntry 102 echo Y>"%TEMP%\%~nx0.Y" 103 call "%~f0" %* <"%TEMP%\%~nx0.Y" 104 rem Use provided errorlevel 105 set RETVAL=%ERRORLEVEL% 106 del /Q "%TEMP%\%~nx0.Y" >NUL 2>&1 107 exit /B %RETVAL% 108 :mainEntry 109 del /Q "%TEMP%\%~nx0.run" >NUL 2>&1 110 111 rem Guess CATALINA_HOME if not defined 112 set "CURRENT_DIR=%cd%" 113 if not "%CATALINA_HOME%" == "" goto gotHome 114 set "CATALINA_HOME=%CURRENT_DIR%" 115 if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome 116 cd .. 117 set "CATALINA_HOME=%cd%" 118 cd "%CURRENT_DIR%" 119 :gotHome 120 121 if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome 122 echo The CATALINA_HOME environment variable is not defined correctly 123 echo This environment variable is needed to run this program 124 goto end 125 :okHome 126 127 rem Copy CATALINA_BASE from CATALINA_HOME if not defined 128 if not "%CATALINA_BASE%" == "" goto gotBase 129 set "CATALINA_BASE=%CATALINA_HOME%" 130 :gotBase 131 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 variables 137 if not exist "%CATALINA_BASE%\bin\setenv.bat" goto checkSetenvHome 138 call "%CATALINA_BASE%\bin\setenv.bat" 139 goto setenvDone 140 :checkSetenvHome 141 if exist "%CATALINA_HOME%\bin\setenv.bat" call "%CATALINA_HOME%\bin\setenv.bat" 142 :setenvDone 143 144 rem Get standard Java environment variables 145 if exist "%CATALINA_HOME%\bin\setclasspath.bat" goto okSetclasspath 146 echo Cannot find "%CATALINA_HOME%\bin\setclasspath.bat" 147 echo This file is needed to run this program 148 goto end 149 :okSetclasspath 150 call "%CATALINA_HOME%\bin\setclasspath.bat" %1 151 if errorlevel 1 goto end 152 153 rem Add on extra jar file to CLASSPATH 154 rem Note that there are no quotes as we do not want to introduce random 155 rem quotes into the CLASSPATH 156 if "%CLASSPATH%" == "" goto emptyClasspath 157 set "CLASSPATH=%CLASSPATH%;" 158 :emptyClasspath 159 set "CLASSPATH=%CLASSPATH%%CATALINA_HOME%\bin\bootstrap.jar" 160 161 if not "%CATALINA_TMPDIR%" == "" goto gotTmpdir 162 set "CATALINA_TMPDIR=%CATALINA_BASE%\temp" 163 :gotTmpdir 164 165 rem Add tomcat-juli.jar to classpath 166 rem tomcat-juli.jar can be over-ridden per instance 167 if not exist "%CATALINA_BASE%\bin\tomcat-juli.jar" goto juliClasspathHome 168 set "CLASSPATH=%CLASSPATH%;%CATALINA_BASE%\bin\tomcat-juli.jar" 169 goto juliClasspathDone 170 :juliClasspathHome 171 set "CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\bin\tomcat-juli.jar" 172 :juliClasspathDone 173 174 if not "%LOGGING_CONFIG%" == "" goto noJuliConfig 175 set LOGGING_CONFIG=-Dnop 176 if not exist "%CATALINA_BASE%\conf\logging.properties" goto noJuliConfig 177 set LOGGING_CONFIG=-Djava.util.logging.config.file="%CATALINA_BASE%\conf\logging.properties" 178 :noJuliConfig 179 set "JAVA_OPTS=%JAVA_OPTS% %LOGGING_CONFIG%" 180 181 if not "%LOGGING_MANAGER%" == "" goto noJuliManager 182 set LOGGING_MANAGER=-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 183 :noJuliManager 184 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_jdk 192 echo Using JRE_HOME: "%JRE_HOME%" 193 goto java_dir_displayed 194 :use_jdk 195 echo Using JAVA_HOME: "%JAVA_HOME%" 196 :java_dir_displayed 197 echo Using CLASSPATH: "%CLASSPATH%" 198 199 set _EXECJAVA=%_RUNJAVA% 200 set MAINCLASS=org.apache.catalina.startup.Bootstrap 201 set ACTION=start 202 set SECURITY_POLICY_FILE= 203 set DEBUG_OPTS= 204 set JPDA= 205 206 if not ""%1"" == ""jpda"" goto noJpda 207 set JPDA=jpda 208 if not "%JPDA_TRANSPORT%" == "" goto gotJpdaTransport 209 set JPDA_TRANSPORT=dt_socket 210 :gotJpdaTransport 211 if not "%JPDA_ADDRESS%" == "" goto gotJpdaAddress 212 set JPDA_ADDRESS=8000 213 :gotJpdaAddress 214 if not "%JPDA_SUSPEND%" == "" goto gotJpdaSuspend 215 set JPDA_SUSPEND=n 216 :gotJpdaSuspend 217 if not "%JPDA_OPTS%" == "" goto gotJpdaOpts 218 set JPDA_OPTS=-agentlib:jdwp=transport=%JPDA_TRANSPORT%,address=%JPDA_ADDRESS%,server=y,suspend=%JPDA_SUSPEND% 219 :gotJpdaOpts 220 shift 221 :noJpda 222 223 if ""%1"" == ""debug"" goto doDebug 224 if ""%1"" == ""run"" goto doRun 225 if ""%1"" == ""start"" goto doStart 226 if ""%1"" == ""stop"" goto doStop 227 if ""%1"" == ""configtest"" goto doConfigTest 228 if ""%1"" == ""version"" goto doVersion 229 230 echo Usage: catalina ( commands ... ) 231 echo commands: 232 echo debug Start Catalina in a debugger 233 echo debug -security Debug Catalina with a security manager 234 echo jpda start Start Catalina under JPDA debugger 235 echo run Start Catalina in the current window 236 echo run -security Start in the current window with security manager 237 echo start Start Catalina in a separate window 238 echo start -security Start in a separate window with security manager 239 echo stop Stop Catalina 240 echo configtest Run a basic syntax check on server.xml 241 echo version What version of tomcat are you running? 242 goto end 243 244 :doDebug 245 shift 246 set _EXECJAVA=%_RUNJDB% 247 set DEBUG_OPTS=-sourcepath "%CATALINA_HOME%\..\..\java" 248 if not ""%1"" == ""-security"" goto execCmd 249 shift 250 echo Using Security Manager 251 set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy" 252 goto execCmd 253 254 :doRun 255 shift 256 if not ""%1"" == ""-security"" goto execCmd 257 shift 258 echo Using Security Manager 259 set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy" 260 goto execCmd 261 262 :doStart 263 shift 264 if "%TITLE%" == "" set TITLE=Tomcat 265 set _EXECJAVA=start "%TITLE%" %_RUNJAVA% 266 if not ""%1"" == ""-security"" goto execCmd 267 shift 268 echo Using Security Manager 269 set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy" 270 goto execCmd 271 272 :doStop 273 shift 274 set ACTION=stop 275 set CATALINA_OPTS= 276 goto execCmd 277 278 :doConfigTest 279 shift 280 set ACTION=configtest 281 set CATALINA_OPTS= 282 goto execCmd 283 284 :doVersion 285 %_EXECJAVA% -classpath "%CATALINA_HOME%\lib\catalina.jar" org.apache.catalina.util.ServerInfo 286 goto end 287 288 289 :execCmd 290 rem Get remaining unshifted command line arguments and save them in the 291 set CMD_LINE_ARGS= 292 :setArgs 293 if ""%1""=="""" goto doneSetArgs 294 set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1 295 shift 296 goto setArgs 297 :doneSetArgs 298 299 rem Execute Java with the applicable properties 300 if not "%JPDA%" == "" goto doJpda 301 if not "%SECURITY_POLICY_FILE%" == "" goto doSecurity 302 %_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 end 304 :doSecurity 305 %_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 end 307 :doJpda 308 if not "%SECURITY_POLICY_FILE%" == "" goto doSecurityJpda 309 %_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 end 311 :doSecurityJpda 312 %_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 end 314 315 :end
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 ======================================2 10 rem Use provided errorlevel 11 set RETVAL=%ERRORLEVEL% 12 echo ====================================="%ERRORLEVEL%" 13 echo ====================================="%RETVAL% 14 del /Q "%TEMP%\%~nx0.Y" >NUL 2>&1 15 exit /B %RETVAL% 16 :mainEntry 17 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 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
line127~130 如果没有CATALINA_BASE这个环境变量的话,把CATALINA_HOME的值给它(CATALINA_HOME这个环境变量我都没有提前配置,CATALINA_BASE肯定也不会配置)。
rem Copy CATALINA_BASE from CATALINA_HOME if not defined if not "%CATALINA_BASE%" == "" goto gotBase set "CATALINA_BASE=%CATALINA_HOME%" :gotBase
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 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
line144~151 调用%CATALINA_HOME%\bin\setclasspath.bat,获取java相关环境变量。
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
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.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 Set JAVA_HOME or JRE_HOME if not already set, ensure any provided settings 19 rem are valid and consistent with the selected start-up options and set up the 20 rem endorsed directory. 21 rem --------------------------------------------------------------------------- 22 23 rem Make sure prerequisite environment variables are set 24 25 rem In debug mode we need a real JDK (JAVA_HOME) 26 if ""%1"" == ""debug"" goto needJavaHome 27 28 rem Otherwise either JRE or JDK are fine 29 if not "%JRE_HOME%" == "" goto gotJreHome 30 if not "%JAVA_HOME%" == "" goto gotJavaHome 31 echo Neither the JAVA_HOME nor the JRE_HOME environment variable is defined 32 echo At least one of these environment variable is needed to run this program 33 goto exit 34 35 :needJavaHome 36 rem Check if we have a usable JDK 37 if "%JAVA_HOME%" == "" goto noJavaHome 38 if not exist "%JAVA_HOME%\bin\java.exe" goto noJavaHome 39 if not exist "%JAVA_HOME%\bin\javaw.exe" goto noJavaHome 40 if not exist "%JAVA_HOME%\bin\jdb.exe" goto noJavaHome 41 if not exist "%JAVA_HOME%\bin\javac.exe" goto noJavaHome 42 set "JRE_HOME=%JAVA_HOME%" 43 goto okJava 44 45 :noJavaHome 46 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 exit 50 51 :gotJavaHome 52 rem No JRE given, use JAVA_HOME as JRE_HOME 53 set "JRE_HOME=%JAVA_HOME%" 54 55 :gotJreHome 56 rem Check if we have a usable JRE 57 if not exist "%JRE_HOME%\bin\java.exe" goto noJreHome 58 if not exist "%JRE_HOME%\bin\javaw.exe" goto noJreHome 59 goto okJava 60 61 :noJreHome 62 rem Needed at least a JRE 63 echo The JRE_HOME environment variable is not defined correctly 64 echo This environment variable is needed to run this program 65 goto exit 66 67 :okJava 68 rem Don‘t override the endorsed dir if the user has set it previously 69 if not "%JAVA_ENDORSED_DIRS%" == "" goto gotEndorseddir 70 rem Set the default -Djava.endorsed.dirs argument 71 set "JAVA_ENDORSED_DIRS=%CATALINA_HOME%\endorsed" 72 :gotEndorseddir 73 74 rem Don‘t override _RUNJAVA if the user has set it previously 75 if not "%_RUNJAVA%" == "" goto gotRunJava 76 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 :gotRunJava 80 81 rem Don‘t override _RUNJDB if the user has set it previously 82 rem Also note the quoting as JAVA_HOME may contain spaces. 83 if not "%_RUNJDB%" == "" goto gotRunJdb 84 set _RUNJDB="%JAVA_HOME%\bin\jdb.exe" 85 :gotRunJdb 86 87 goto end 88 89 :exit 90 exit /b 1 91 92 :end 93 exit /b 0
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 CLASSPATH 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"
line161~163 如果没有%CATALINA_TMPDIR%环境变量的话,设置%CATALINA_TMPDIR%=%CATALINA_BASE%\temp。
if not "%CATALINA_TMPDIR%" == "" goto gotTmpdir set "CATALINA_TMPDIR=%CATALINA_BASE%\temp" :gotTmpdir
line165~172 把tomcat-juli.jar写进CLASSPATH里。
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
line174~184 对日志的处理,给%JAVA_OPTS%加上了%LOGGING_CONFIG% %LOGGING_MANAGER%。
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" :noJuliConfig set "JAVA_OPTS=%JAVA_OPTS% %LOGGING_CONFIG%" if not "%LOGGING_MANAGER%" == "" goto noJuliManager set LOGGING_MANAGER=-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager :noJuliManager set "JAVA_OPTS=%JAVA_OPTS% %LOGGING_MANAGER%"
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_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%"
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.Bootstrap set ACTION=start set SECURITY_POLICY_FILE= set DEBUG_OPTS= set JPDA=
line206~221 是对jdpa的一些操作,搜了一下jdpa的简介,是调试java的api。
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
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 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 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
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赋值。
: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
line254~260 doRun节点,如果启动时第二个参数是-security,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
line262~270 doStart节点。设置TITLE=TOMCAT,设置_EXECJAVA=start "TOMCAT" %_RUNJAVA%,这样cmd窗口的title会变为“TOMCAT”。如果启动时第二个参数是-security,set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy";否则直接goto execCmd节点。
:doStart shift if "%TITLE%" == "" set TITLE=Tomcat set _EXECJAVA=start "%TITLE%" %_RUNJAVA% if not ""%1"" == ""-security"" goto execCmd shift echo Using Security Manager set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy" goto execCmd
line272~276 doStop节点,改变ACTION=stop,CATALINA_OPTS=,goto execCmd节点。
:doStop shift set ACTION=stop set CATALINA_OPTS= goto execCmd
line278~282 doConfigTest节点,改变ACTION=configtest,CATALINA_OPTS=,goto execCmd节点。
:doConfigTest shift set ACTION=configtest set CATALINA_OPTS= goto execCmd
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.ServerInfo goto end
反编译后的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 properties if not "%JPDA%" == "" goto doJpda if 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 :doJpda if 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
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。
标签:
原文地址:http://www.cnblogs.com/yizhishi/p/5936149.html