windows是.bat,linux就是.sh了,差不多一个意思。
启动脚本做的事情,大多是系统环境变量的判断,在所有前置条件满足的情况下执行Tomcat 的源码的启动类。
1 @echo off 16 17 if "%OS%" == "Windows_NT" setlocal 23 25 set "CURRENT_DIR=%cd%" 26 if not "%CATALINA_HOME%" == "" goto gotHome 27 set "CATALINA_HOME=%CURRENT_DIR%" 28 if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome 29 cd .. 30 set "CATALINA_HOME=%cd%" 31 cd "%CURRENT_DIR%" 32 :gotHome 33 if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome 34 echo The CATALINA_HOME environment variable is not defined correctly 35 echo This environment variable is needed to run this program 36 goto end 37 :okHome ### 上面是各种系统变量的判断,下面这个为定义catalina.bat文件的路径(会用到) 39 set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat" 40 42 if exist "%EXECUTABLE%" goto okExec 43 echo Cannot find "%EXECUTABLE%" 44 echo This file is needed to run this program 45 goto end 46 :okExec 47 49 set CMD_LINE_ARGS= 50 :setArgs 51 if ""%1""=="""" goto doneSetArgs 52 set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1 53 shift 54 goto setArgs ### 48 到 55 行将运行脚本前如果设置了其他参数,将参数保存到 CMD_LINE_ARGS 变量中 55 :doneSetArgs 56 57 call "%EXECUTABLE%" start %CMD_LINE_ARGS% 58 59 :end
设置一些系统执行脚本需要的环境变量,以及最重要的
CATALINA_HOME
环境变量【这里的值是tomcat的bin目录下的catalina.bat 文件】最后会执行catalina.bat文件,带上参数
start
@echo off 16 17 if "%OS%" == "Windows_NT" setlocal 98 100 if not ""%1"" == ""run"" goto mainEntry 101 if "%TEMP%" == "" goto mainEntry 102 if exist "%TEMP%\%~nx0.run" goto mainEntry 103 echo Y>"%TEMP%\%~nx0.run" 104 if not exist "%TEMP%\%~nx0.run" goto mainEntry 105 echo Y>"%TEMP%\%~nx0.Y" 106 call "%~f0" %* <"%TEMP%\%~nx0.Y" 108 set RETVAL=%ERRORLEVEL% 109 del /Q "%TEMP%\%~nx0.Y" >NUL 2>&1 110 exit /B %RETVAL% 111 :mainEntry 112 del /Q "%TEMP%\%~nx0.run" >NUL 2>&1 113 115 set "CURRENT_DIR=%cd%" 116 if not "%CATALINA_HOME%" == "" goto gotHome 117 set "CATALINA_HOME=%CURRENT_DIR%" 118 if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome 119 cd .. 120 set "CATALINA_HOME=%cd%" 121 cd "%CURRENT_DIR%" 122 :gotHome ### CATALINA_HOME变量,这里又验证了一次,防止用户直接运行 catalina.bat 文件 124 if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome 125 echo The CATALINA_HOME environment variable is not defined correctly 126 echo This environment variable is needed to run this program 127 goto end 128 :okHome 129 131 if not "%CATALINA_BASE%" == "" goto gotBase 132 set "CATALINA_BASE=%CATALINA_HOME%" 133 :gotBase 134 137 set CLASSPATH= 138 140 if not exist "%CATALINA_BASE%\bin\setenv.bat" goto checkSetenvHome 141 call "%CATALINA_BASE%\bin\setenv.bat" 142 goto setenvDone 143 :checkSetenvHome 144 if exist "%CATALINA_HOME%\bin\setenv.bat" call "%CATALINA_HOME%\bin\setenv.bat" 145 :setenvDone 146 148 if exist "%CATALINA_HOME%\bin\setclasspath.bat" goto okSetclasspath 149 echo Cannot find "%CATALINA_HOME%\bin\setclasspath.bat" 150 echo This file is needed to run this program 151 goto end 152 :okSetclasspath 153 call "%CATALINA_HOME%\bin\setclasspath.bat" %1 154 if errorlevel 1 goto end ### setclasspath.bat 主要功能校验是否设置JAVA_HOME变量,没有则在控制台显示出提示信息。注意该文件中第 79 行将会设置_RUNJAVA变量,该变量的值即JAVA_HOME或JRE_HOME的 bin 目录下的 java.exe 文件 159 if "%CLASSPATH%" == "" goto emptyClasspath 160 set "CLASSPATH=%CLASSPATH%;" 161 :emptyClasspath 162 set "CLASSPATH=%CLASSPATH%%CATALINA_HOME%\bin\bootstrap.jar" 163 164 if not "%CATALINA_TMPDIR%" == "" goto gotTmpdir 165 set "CATALINA_TMPDIR=%CATALINA_BASE%\temp" 166 :gotTmpdir 167 170 if not exist "%CATALINA_BASE%\bin\tomcat-juli.jar" goto juliClasspathHome 171 set "CLASSPATH=%CLASSPATH%;%CATALINA_BASE%\bin\tomcat-juli.jar" 172 goto juliClasspathDone 173 :juliClasspathHome 174 set "CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\bin\tomcat-juli.jar" 175 :juliClasspathDone 176 177 if not "%LOGGING_CONFIG%" == "" goto noJuliConfig 178 set LOGGING_CONFIG=-Dnop 179 if not exist "%CATALINA_BASE%\conf\logging.properties" goto noJuliConfig 180 set LOGGING_CONFIG=-Djava.util.logging.config.file="%CATALINA_BASE%\conf\logging.properties" 181 :noJuliConfig 182 set JAVA_OPTS=%JAVA_OPTS% %LOGGING_CONFIG% 183 184 if not "%LOGGING_MANAGER%" == "" goto noJuliManager 185 set LOGGING_MANAGER=-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 186 :noJuliManager 187 set JAVA_OPTS=%JAVA_OPTS% %LOGGING_MANAGER% 188 191 echo Using CATALINA_BASE: "%CATALINA_BASE%" 192 echo Using CATALINA_HOME: "%CATALINA_HOME%" 193 echo Using CATALINA_TMPDIR: "%CATALINA_TMPDIR%" 194 if ""%1"" == ""debug"" goto use_jdk 195 echo Using JRE_HOME: "%JRE_HOME%" 196 goto java_dir_displayed 197 :use_jdk 198 echo Using JAVA_HOME: "%JAVA_HOME%" 199 :java_dir_displayed 200 echo Using CLASSPATH: "%CLASSPATH%" 201 202 set _EXECJAVA=%_RUNJAVA% ### ※ 203 set MAINCLASS=org.apache.catalina.startup.Bootstrap ### ※ 204 set ACTION=start ### ※ ### 这3个变量最后启动分析时将会用到 205 set SECURITY_POLICY_FILE= 206 set DEBUG_OPTS= 207 set JPDA= 208 209 if not ""%1"" == ""jpda"" goto noJpda 210 set JPDA=jpda 211 if not "%JPDA_TRANSPORT%" == "" goto gotJpdaTransport 212 set JPDA_TRANSPORT=dt_socket 213 :gotJpdaTransport 214 if not "%JPDA_ADDRESS%" == "" goto gotJpdaAddress 215 set JPDA_ADDRESS=8000 216 :gotJpdaAddress 217 if not "%JPDA_SUSPEND%" == "" goto gotJpdaSuspend 218 set JPDA_SUSPEND=n 219 :gotJpdaSuspend 220 if not "%JPDA_OPTS%" == "" goto gotJpdaOpts 221 set JPDA_OPTS=-agentlib:jdwp=transport=%JPDA_TRANSPORT%,address=%JPDA_ADDRESS%,server=y,suspend=%JPDA_SUSPEND% 222 :gotJpdaOpts 223 shift 224 :noJpda 225 226 if ""%1"" == ""debug"" goto doDebug 227 if ""%1"" == ""run"" goto doRun 228 if ""%1"" == ""start"" goto doStart 229 if ""%1"" == ""stop"" goto doStop 230 if ""%1"" == ""configtest"" goto doConfigTest 231 if ""%1"" == ""version"" goto doVersion 232 233 echo Usage: catalina ( commands ... ) 234 echo commands: 235 echo debug Start Catalina in a debugger 236 echo debug -security Debug Catalina with a security manager 237 echo jpda start Start Catalina under JPDA debugger 238 echo run Start Catalina in the current window 239 echo run -security Start in the current window with security manager 240 echo start Start Catalina in a separate window 241 echo start -security Start in a separate window with security manager 242 echo stop Stop Catalina 243 echo configtest Run a basic syntax check on server.xml 244 echo version What version of tomcat are you running? 245 goto end 246 247 :doDebug 248 shift 249 set _EXECJAVA=%_RUNJDB% 250 set DEBUG_OPTS=-sourcepath "%CATALINA_HOME%\..\..\java" 251 if not ""%1"" == ""-security"" goto execCmd 252 shift 253 echo Using Security Manager 254 set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy" 255 goto execCmd 256 257 :doRun 258 shift 259 if not ""%1"" == ""-security"" goto execCmd 260 shift 261 echo Using Security Manager 262 set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy" 263 goto execCmd 264 265 :doStart 266 shift ### 校验和设置一些变量 267 if not "%OS%" == "Windows_NT" goto noTitle 268 if "%TITLE%" == "" set TITLE=Tomcat 269 set _EXECJAVA=start "%TITLE%" %_RUNJAVA% 270 goto gotTitle 271 :noTitle 272 set _EXECJAVA=start %_RUNJAVA% 273 :gotTitle 274 if not ""%1"" == ""-security"" goto execCmd 275 shift 276 echo Using Security Manager 277 set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy" 278 goto execCmd 279 280 :doStop 281 shift 282 set ACTION=stop 283 set CATALINA_OPTS= 284 goto execCmd 285 286 :doConfigTest 287 shift 288 set ACTION=configtest 289 set CATALINA_OPTS= 290 goto execCmd 291 292 :doVersion 293 %_EXECJAVA% -classpath "%CATALINA_HOME%\lib\catalina.jar" org.apache.catalina.util.ServerInfo 294 goto end 295 296 297 :execCmd 299 set CMD_LINE_ARGS= 300 :setArgs 301 if ""%1""=="""" goto doneSetArgs 302 set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1 303 shift 304 goto setArgs 305 :doneSetArgs 306 308 if not "%JPDA%" == "" goto doJpda 309 if not "%SECURITY_POLICY_FILE%" == "" goto doSecurity 310 %_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% ### 311 goto end 312 :doSecurity 313 %_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% ### %_EXECJAVA% == start Tomcat java.exe ### %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% 。。。 == jvm 内存等运行参数和系统属性 ### 启动单独窗口 名字Tomcat 运行java.exe CATALINA_OPTS ### %MAINCLASS% == org.apache.catalina.startup.Bootstrap ### %CMD_LINE_ARGS% == 命令行参数 ### %ACTION% == start ### 最终就是:javac Bootstrap.main(..,start) -jvm..配置。。。 314 goto end 315 :doJpda 316 if not "%SECURITY_POLICY_FILE%" == "" goto doSecurityJpda 317 %_EXECJAVA% %JAVA_OPTS% %CATALINA_OPTS% %JPDA_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% 318 goto end 319 :doSecurityJpda 320 %_EXECJAVA% %JAVA_OPTS% %CATALINA_OPTS% %JPDA_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% 321 goto end 322 323 :end
还是对一些变量环境参数做了一些条件的判断,以及执行了
setenv.bat
和setclasspath.bat
文件最终,执行 org.apache.catalina.startup.Bootstrap的main方法,同时带上参数 start;
【以不同参数启动tamcat,都是到这个main方法,只是添加了不同了的如参;】
分析资料:http://www.iocoder.cn/Tomcat/yuliu/Start-analysis-1-start-script/
源码调试【用IDEA调试tomcat源码】 https://blog.csdn.net/mrhuangxiutao/article/details/78261155
小杭
邮箱:[email protected]