[关闭]
@w1992wishes 2018-02-13T09:32:40.000000Z 字数 25464 阅读 1352

Tomcat源码分析 -- Tomcat的启动过程(一)

源码分析 tomcat


本篇结构:

一、前言

一般启动tomcat都是找到bin目录下的startup.bat(windows)下或者startup.sh(linux下),所以要研究tomcat的启动,startup脚本是避不开的。那么下面就来看一看windows下面的startup.bat。

二、bat文件

要看懂startup.bat,首先得对bat文件有个基本的了解。

bat文件是dos下的批处理文件。批处理文件是无格式的文本文件,这个文件的每一行都是一条DOS命令。它的文件扩展名为 .bat 或 .cmd。在命令提示下键入批处理文件的名称,或者双击该批处理文件,系统就会调用cmd.exe按照该文件中各个命令出现的顺序来逐个运行它们。使用批处理文件(也被称为批处理程序或脚本),可以简化日常或重复性任务。

批处理简单语法:

DOS命令 解释
rem 该命令用于注释,rem起始的行不会作为代码执行
pause 该命令用于暂停正在执行的批处理文件,并且提示用户按键,然后程序继续执行
echo 该命令用于在dos控制台显示一段文本,相当于print,如果想要显示环境变量需要在环境变量前后加上%,例如显示操作系统echo %OS%
echo off 该命令可以防止将批处理文件中的具体命令打印出来,而只会输出执行结果
@echo off 该命令与echo off相同,唯一的区别在于@echo off不仅会隐藏具体命令还会连'echo off'这个自身命令也隐藏起来
set 设置环境变量,例如set A = 100设置A变量为100
label 使用':'(冒号)来设置一个标签,供给goto命令使用,例如":init"代表一个init标签
goto 该命令使正在执行的命令强制跳转到他指定的标签。例如我需要跳转指定A标签下的命令,如下:goto A
not 该命令用来取反,相当于逻辑非
if 该命令表示判断
exist 该命令通常用来测试文件是否存在,一般和if一起使用
shift 该命令用来将参数后移一位即将%2%赋值给%1%,%3%赋值给%2%,也可以理解为参数列表左移即删除现有参数列表的第一位
call 该命令用来调用另外一条命令
setLocal 该命令表示该批处理文件中修改的环境变量只在本文件中起作用,或者直到endLocal命令出现,被修改的环境变量才恢复原状
start 重新开启一个dos窗口

可以参考一下这篇博文:

Bat命令学习

三、startup.bat

用编辑器打开startup.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. rem ---------------------------------------------------------------------------
  17. rem Start script for the CATALINA Server
  18. rem ---------------------------------------------------------------------------
  19. setlocal
  20. rem Guess CATALINA_HOME if not defined
  21. set "CURRENT_DIR=%cd%"
  22. if not "%CATALINA_HOME%" == "" goto gotHome
  23. set "CATALINA_HOME=%CURRENT_DIR%"
  24. if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
  25. cd ..
  26. set "CATALINA_HOME=%cd%"
  27. cd "%CURRENT_DIR%"
  28. :gotHome
  29. if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
  30. echo The CATALINA_HOME environment variable is not defined correctly
  31. echo This environment variable is needed to run this program
  32. goto end
  33. :okHome
  34. set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat"
  35. rem Check that target executable exists
  36. if exist "%EXECUTABLE%" goto okExec
  37. echo Cannot find "%EXECUTABLE%"
  38. echo This file is needed to run this program
  39. goto end
  40. :okExec
  41. rem Get remaining unshifted command line arguments and save them in the
  42. set CMD_LINE_ARGS=
  43. :setArgs
  44. if ""%1""=="""" goto doneSetArgs
  45. set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
  46. shift
  47. goto setArgs
  48. :doneSetArgs
  49. call "%EXECUTABLE%" start %CMD_LINE_ARGS%
  50. :end

@echo off,此语句常用于开头,表示不显示所有的命令行信息,包括此句。

然后是一大段rem后面的内容,是注释,对软件的License做了说明,英文不太好,就不翻译了。

setlocal,启动批处理文件中环境变量的本地化,本地化将持续到出现匹配的endlocal命令或者到达批处理文件结尾为止。

下面将批处理文件拆成几段看:

第一段:判断CATALINA_HOME是否定义

  1. rem Guess CATALINA_HOME if not defined
  2. set "CURRENT_DIR=%cd%"
  3. if not "%CATALINA_HOME%" == "" goto gotHome
  4. set "CATALINA_HOME=%CURRENT_DIR%"
  5. if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
  6. cd ..
  7. set "CATALINA_HOME=%cd%"
  8. cd "%CURRENT_DIR%"
  1. 设置变量CURRENT_DIR为当前目录,一般打开的就是tomcat的bin目录,./apache-tomcat-x.x.xx/bin;
  2. 判断系统变量CATALINA_HOME是不是空串(一般安装tomcat时,我们都会配置CATALINA_HOME环境变量,不知有没有印象呢),如果是就跳转到gotHome标签执行,假设CATALINA_HOME环境变量没有定义,就继续往下;
  3. 设置CATALINA_HOME为CURRENT_DIR指向的目录,也就是bin目录(./apache-tomcat-x.x.xx/bin);
  4. 判断CATALINA_HOME\bin\catalina.bat文件是否存在,也就是 ./apache-tomcat-x.x.xx/bin/bin/catalina.bat,如果存在就转向okHome标签,显然是不存在的,所以不会跳转到okHome,继续往下;
  5. 不存在的话,CATALINA_HOME取上级目录的值,也就是(./apache-tomcat-x.x.xx/);
  6. 设置CATALINA_HOME指向当前目录,也就是Tomcat的根目录;
  7. 进入 CURRENT_DIR(./apache-tomcat-x.x.xx/bin);

第二段:执行gotHome标签

  1. :gotHome
  2. if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
  3. echo The CATALINA_HOME environment variable is not defined correctly
  4. echo This environment variable is needed to run this program
  5. goto end

通过上面的设置,CATALINA_HOME的值已经是: ./apache-tomcat-x.x.xx/,这时判断./apache-tomcat-x.x.xx/catalina.bat 脚本是否存在,如果不存在,则输出一些信息结束,一般这时是可以找到的,所以跳转到okHome标签处;

第三段:执行okHome标签

  1. :okHome
  2. set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat"

设置EXECUTABLE变量指向为catalina.bat脚本;

第四段:再次检查catalina.bat是否存在

  1. rem Check that target executable exists
  2. if exist "%EXECUTABLE%" goto okExec
  3. echo Cannot find "%EXECUTABLE%"
  4. echo This file is needed to run this program
  5. goto end

双重保险继续判断下EXECUTABLE指向的catalina.bat文件是否存在,不存在就输出提示信息并且结束,存在就跳转到okExec标签处:

第五段:

  1. :okExec
  2. rem Get remaining unshifted command line arguments and save them in the
  3. set CMD_LINE_ARGS=
  4. :setArgs
  5. if ""%1""=="""" goto doneSetArgs
  6. set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
  7. shift
  8. goto setArgs
  9. :doneSetArgs
  10. call "%EXECUTABLE%" start %CMD_LINE_ARGS%
  1. set CMD_LINE_ARGS=表示清空变量CMD_LINE_ARGS;
  2. 执行setArgs标签;
  3. 第一个变量(%1%)为空字符串,那么就跳转到doneSetArgs标签,一般情况下,直接运行startup.bat,是没有传递任何参数,所以是跳转到doneSetArgs标签,由此也可以猜想出,如果不是使用双击执行的话,使用命令行启动startup.bat那么是可以传递参数的;
  4. 跳转到doneSetArgs,调用EXECUTABLE指向的文件,也就是catalina.bat文件,同时传递start参数,因为CMD_LINE_ARGS为空,所以只传递了一个start参数;

**最后:**end标签

  1. :end

判断失败跳转的标签,是参数不正确的时候的结束标志。

四、catalina.bat

如果startup.bat执行正确,就等同于运行catalina.bat start,进入了catalina.bat脚本中。

为了减少抵触情绪,将脚本前面的一大段注释删掉了,如下:

  1. @echo off
  2. setlocal
  3. rem Suppress Terminate batch job on CTRL+C
  4. if not ""%1"" == ""run"" goto mainEntry
  5. if "%TEMP%" == "" goto mainEntry
  6. if exist "%TEMP%\%~nx0.run" goto mainEntry
  7. echo Y>"%TEMP%\%~nx0.run"
  8. if not exist "%TEMP%\%~nx0.run" goto mainEntry
  9. echo Y>"%TEMP%\%~nx0.Y"
  10. call "%~f0" %* <"%TEMP%\%~nx0.Y"
  11. rem Use provided errorlevel
  12. set RETVAL=%ERRORLEVEL%
  13. del /Q "%TEMP%\%~nx0.Y" >NUL 2>&1
  14. exit /B %RETVAL%
  15. :mainEntry
  16. del /Q "%TEMP%\%~nx0.run" >NUL 2>&1
  17. rem Guess CATALINA_HOME if not defined
  18. set "CURRENT_DIR=%cd%"
  19. if not "%CATALINA_HOME%" == "" goto gotHome
  20. set "CATALINA_HOME=%CURRENT_DIR%"
  21. if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
  22. cd ..
  23. set "CATALINA_HOME=%cd%"
  24. cd "%CURRENT_DIR%"
  25. :gotHome
  26. if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
  27. echo The CATALINA_HOME environment variable is not defined correctly
  28. echo This environment variable is needed to run this program
  29. goto end
  30. :okHome
  31. rem Copy CATALINA_BASE from CATALINA_HOME if not defined
  32. if not "%CATALINA_BASE%" == "" goto gotBase
  33. set "CATALINA_BASE=%CATALINA_HOME%"
  34. :gotBase
  35. rem Ensure that neither CATALINA_HOME nor CATALINA_BASE contains a semi-colon
  36. rem as this is used as the separator in the classpath and Java provides no
  37. rem mechanism for escaping if the same character appears in the path. Check this
  38. rem by replacing all occurrences of ';' with '' and checking that neither
  39. rem CATALINA_HOME nor CATALINA_BASE have changed
  40. if "%CATALINA_HOME%" == "%CATALINA_HOME:;=%" goto homeNoSemicolon
  41. echo Using CATALINA_HOME: "%CATALINA_HOME%"
  42. echo Unable to start as CATALINA_HOME contains a semicolon (;) character
  43. goto end
  44. :homeNoSemicolon
  45. if "%CATALINA_BASE%" == "%CATALINA_BASE:;=%" goto baseNoSemicolon
  46. echo Using CATALINA_BASE: "%CATALINA_BASE%"
  47. echo Unable to start as CATALINA_BASE contains a semicolon (;) character
  48. goto end
  49. :baseNoSemicolon
  50. rem Ensure that any user defined CLASSPATH variables are not used on startup,
  51. rem but allow them to be specified in setenv.bat, in rare case when it is needed.
  52. set CLASSPATH=
  53. rem Get standard environment variables
  54. if not exist "%CATALINA_BASE%\bin\setenv.bat" goto checkSetenvHome
  55. call "%CATALINA_BASE%\bin\setenv.bat"
  56. goto setenvDone
  57. :checkSetenvHome
  58. if exist "%CATALINA_HOME%\bin\setenv.bat" call "%CATALINA_HOME%\bin\setenv.bat"
  59. :setenvDone
  60. rem Get standard Java environment variables
  61. if exist "%CATALINA_HOME%\bin\setclasspath.bat" goto okSetclasspath
  62. echo Cannot find "%CATALINA_HOME%\bin\setclasspath.bat"
  63. echo This file is needed to run this program
  64. goto end
  65. :okSetclasspath
  66. call "%CATALINA_HOME%\bin\setclasspath.bat" %1
  67. if errorlevel 1 goto end
  68. rem Add on extra jar file to CLASSPATH
  69. rem Note that there are no quotes as we do not want to introduce random
  70. rem quotes into the CLASSPATH
  71. if "%CLASSPATH%" == "" goto emptyClasspath
  72. set "CLASSPATH=%CLASSPATH%;"
  73. :emptyClasspath
  74. set "CLASSPATH=%CLASSPATH%%CATALINA_HOME%\bin\bootstrap.jar"
  75. if not "%CATALINA_TMPDIR%" == "" goto gotTmpdir
  76. set "CATALINA_TMPDIR=%CATALINA_BASE%\temp"
  77. :gotTmpdir
  78. rem Add tomcat-juli.jar to classpath
  79. rem tomcat-juli.jar can be over-ridden per instance
  80. if not exist "%CATALINA_BASE%\bin\tomcat-juli.jar" goto juliClasspathHome
  81. set "CLASSPATH=%CLASSPATH%;%CATALINA_BASE%\bin\tomcat-juli.jar"
  82. goto juliClasspathDone
  83. :juliClasspathHome
  84. set "CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\bin\tomcat-juli.jar"
  85. :juliClasspathDone
  86. if not "%JSSE_OPTS%" == "" goto gotJsseOpts
  87. set JSSE_OPTS="-Djdk.tls.ephemeralDHKeySize=2048"
  88. :gotJsseOpts
  89. set "JAVA_OPTS=%JAVA_OPTS% %JSSE_OPTS%"
  90. rem Register custom URL handlers
  91. rem Do this here so custom URL handles (specifically 'war:...') can be used in the security policy
  92. set "JAVA_OPTS=%JAVA_OPTS% -Djava.protocol.handler.pkgs=org.apache.catalina.webresources"
  93. if not "%LOGGING_CONFIG%" == "" goto noJuliConfig
  94. set LOGGING_CONFIG=-Dnop
  95. if not exist "%CATALINA_BASE%\conf\logging.properties" goto noJuliConfig
  96. set LOGGING_CONFIG=-Djava.util.logging.config.file="%CATALINA_BASE%\conf\logging.properties"
  97. :noJuliConfig
  98. if not "%LOGGING_MANAGER%" == "" goto noJuliManager
  99. set LOGGING_MANAGER=-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
  100. :noJuliManager
  101. rem Configure JAVA 9 specific start-up parameters
  102. set "JDK_JAVA_OPTIONS=%JDK_JAVA_OPTIONS% --add-opens=java.base/java.lang=ALL-UNNAMED"
  103. set "JDK_JAVA_OPTIONS=%JDK_JAVA_OPTIONS% --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED"
  104. rem Java 9 no longer supports the java.endorsed.dirs
  105. rem system property. Only try to use it if
  106. rem JAVA_ENDORSED_DIRS was explicitly set
  107. rem or CATALINA_HOME/endorsed exists.
  108. set ENDORSED_PROP=ignore.endorsed.dirs
  109. if "%JAVA_ENDORSED_DIRS%" == "" goto noEndorsedVar
  110. set ENDORSED_PROP=java.endorsed.dirs
  111. goto doneEndorsed
  112. :noEndorsedVar
  113. if not exist "%CATALINA_HOME%\endorsed" goto doneEndorsed
  114. set ENDORSED_PROP=java.endorsed.dirs
  115. :doneEndorsed
  116. rem ----- Execute The Requested Command ---------------------------------------
  117. echo Using CATALINA_BASE: "%CATALINA_BASE%"
  118. echo Using CATALINA_HOME: "%CATALINA_HOME%"
  119. echo Using CATALINA_TMPDIR: "%CATALINA_TMPDIR%"
  120. if ""%1"" == ""debug"" goto use_jdk
  121. echo Using JRE_HOME: "%JRE_HOME%"
  122. goto java_dir_displayed
  123. :use_jdk
  124. echo Using JAVA_HOME: "%JAVA_HOME%"
  125. :java_dir_displayed
  126. echo Using CLASSPATH: "%CLASSPATH%"
  127. set _EXECJAVA=%_RUNJAVA%
  128. set MAINCLASS=org.apache.catalina.startup.Bootstrap
  129. set ACTION=start
  130. set SECURITY_POLICY_FILE=
  131. set DEBUG_OPTS=
  132. set JPDA=
  133. if not ""%1"" == ""jpda"" goto noJpda
  134. set JPDA=jpda
  135. if not "%JPDA_TRANSPORT%" == "" goto gotJpdaTransport
  136. set JPDA_TRANSPORT=dt_socket
  137. :gotJpdaTransport
  138. if not "%JPDA_ADDRESS%" == "" goto gotJpdaAddress
  139. set JPDA_ADDRESS=localhost:8000
  140. :gotJpdaAddress
  141. if not "%JPDA_SUSPEND%" == "" goto gotJpdaSuspend
  142. set JPDA_SUSPEND=n
  143. :gotJpdaSuspend
  144. if not "%JPDA_OPTS%" == "" goto gotJpdaOpts
  145. set JPDA_OPTS=-agentlib:jdwp=transport=%JPDA_TRANSPORT%,address=%JPDA_ADDRESS%,server=y,suspend=%JPDA_SUSPEND%
  146. :gotJpdaOpts
  147. shift
  148. :noJpda
  149. if ""%1"" == ""debug"" goto doDebug
  150. if ""%1"" == ""run"" goto doRun
  151. if ""%1"" == ""start"" goto doStart
  152. if ""%1"" == ""stop"" goto doStop
  153. if ""%1"" == ""configtest"" goto doConfigTest
  154. if ""%1"" == ""version"" goto doVersion
  155. echo Usage: catalina ( commands ... )
  156. echo commands:
  157. echo debug Start Catalina in a debugger
  158. echo debug -security Debug Catalina with a security manager
  159. echo jpda start Start Catalina under JPDA debugger
  160. echo run Start Catalina in the current window
  161. echo run -security Start in the current window with security manager
  162. echo start Start Catalina in a separate window
  163. echo start -security Start in a separate window with security manager
  164. echo stop Stop Catalina
  165. echo configtest Run a basic syntax check on server.xml
  166. echo version What version of tomcat are you running?
  167. goto end
  168. :doDebug
  169. shift
  170. set _EXECJAVA=%_RUNJDB%
  171. set DEBUG_OPTS=-sourcepath "%CATALINA_HOME%\..\..\java"
  172. if not ""%1"" == ""-security"" goto execCmd
  173. shift
  174. echo Using Security Manager
  175. set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
  176. goto execCmd
  177. :doRun
  178. shift
  179. if not ""%1"" == ""-security"" goto execCmd
  180. shift
  181. echo Using Security Manager
  182. set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
  183. goto execCmd
  184. :doStart
  185. shift
  186. if "%TITLE%" == "" set TITLE=Tomcat
  187. set _EXECJAVA=start "%TITLE%" %_RUNJAVA%
  188. if not ""%1"" == ""-security"" goto execCmd
  189. shift
  190. echo Using Security Manager
  191. set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
  192. goto execCmd
  193. :doStop
  194. shift
  195. set ACTION=stop
  196. set CATALINA_OPTS=
  197. goto execCmd
  198. :doConfigTest
  199. shift
  200. set ACTION=configtest
  201. set CATALINA_OPTS=
  202. goto execCmd
  203. :doVersion
  204. %_EXECJAVA% -classpath "%CATALINA_HOME%\lib\catalina.jar" org.apache.catalina.util.ServerInfo
  205. goto end
  206. :execCmd
  207. rem Get remaining unshifted command line arguments and save them in the
  208. set CMD_LINE_ARGS=
  209. :setArgs
  210. if ""%1""=="""" goto doneSetArgs
  211. set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
  212. shift
  213. goto setArgs
  214. :doneSetArgs
  215. rem Execute Java with the applicable properties
  216. if not "%JPDA%" == "" goto doJpda
  217. if not "%SECURITY_POLICY_FILE%" == "" goto doSecurity
  218. %_EXECJAVA% %LOGGING_CONFIG% %LOGGING_MANAGER% %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -D%ENDORSED_PROP%="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
  219. goto end
  220. :doSecurity
  221. %_EXECJAVA% %LOGGING_CONFIG% %LOGGING_MANAGER% %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -D%ENDORSED_PROP%="%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%
  222. goto end
  223. :doJpda
  224. if not "%SECURITY_POLICY_FILE%" == "" goto doSecurityJpda
  225. %_EXECJAVA% %LOGGING_CONFIG% %LOGGING_MANAGER% %JAVA_OPTS% %JPDA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -D%ENDORSED_PROP%="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
  226. goto end
  227. :doSecurityJpda
  228. %_EXECJAVA% %LOGGING_CONFIG% %LOGGING_MANAGER% %JAVA_OPTS% %JPDA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -D%ENDORSED_PROP%="%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%
  229. goto end
  230. :end

和startup.bat脚本一样,一段段分析:

第一段:

  1. rem Suppress Terminate batch job on CTRL+C
  2. if not ""%1"" == ""run"" goto mainEntry
  3. if "%TEMP%" == "" goto mainEntry
  4. if exist "%TEMP%\%~nx0.run" goto mainEntry
  5. echo Y>"%TEMP%\%~nx0.run"
  6. if not exist "%TEMP%\%~nx0.run" goto mainEntry
  7. echo Y>"%TEMP%\%~nx0.Y"
  8. call "%~f0" %* <"%TEMP%\%~nx0.Y"
  9. rem Use provided errorlevel
  10. set RETVAL=%ERRORLEVEL%
  11. del /Q "%TEMP%\%~nx0.Y" >NUL 2>&1
  12. exit /B %RETVAL%
  13. :mainEntry
  14. del /Q "%TEMP%\%~nx0.run" >NUL 2>&1
  1. 首先注释是说,禁止使用 CTRL+C 来终止批处理任务;
  2. if not ""%1"" == ""run"" goto mainEntry,第一个参数不是run,就转向mainEntry标签,通过调用startup.bat启动的第一个参数应该是start;
  3. if "%TEMP%" == "" goto mainEntry,%TEMP%是系统的环境变量值,通常装完windows系统的话, 系统会自动配置上这个环境变量,一般是C:\Users\用户名\AppData\Local\Temp(AppData是一个隐藏文件夹)。这句是说,如果%TEMP%是空串,就转向mainEntry标签;
  4. if exist "%TEMP%\%~nx0.run" goto mainEntry, 在批处理中,%1 表示的是程序之后的第一个参数,%0表示这个可执行程序的名称, %~nx0 的话就是程序的名称+扩展名,在这里就是catalina.bat(可以写一个脚本测试下);
  5. echo Y>"%TEMP%\%~nx0.run",写入字符Y到%TEMP%\catalina.bat.run 文件中;
  6. if not exist "%TEMP%\%~nx0.run" goto mainEntry,又判断了一下 %TEMP%\catalina.bat.run 文件是否存在;
  7. echo Y>"%TEMP%\%~nx0.Y",写入Y到%TEMP%\catalina.bat.Y,如果文件不存在, 则新建一个;
  8. call "%~f0" %* <"%TEMP%\%~nx0.Y",- "%~f0" : 简单说就是表示当前命令的绝对路径。- "%" : %1表示第一个参数,%2表示第二个,%代表所有参数。<"%TEMP%\%~nx0.Y"是读取 %TEMP%\catalina.bat.Y文件中的内容,之后又通过call进行调用;
  9. set RETVAL=%ERRORLEVEL%,Windows中和linux一样,命令执行完之后都有自己的退出码,%ERRORLEVEL%就是取的上面的call 命令的退出码,赋值给一个变量RETVAL;
  10. del /Q "%TEMP%\%~nx0.Y" >NUL 2>&1,del 命令,很容易联想到delete, 那么/Q是什么意思呢?静默删除,不会给出任何提示,就比如Linux中的rm -f 一样。这里是删除%TEMP%\catalina.bat.Y这个文件。后面的 >NUL 2>&1又是什么意思呢? >NUL : 表示将输出重定向到 NUL 中,你什么也看不到;2>&1 : 2:错误输出,&1: 标准输出, 意思就是将错误消息输出到标准输出中;>NUL 2>&1 : 就是先将错误消息输出到标准输出中,然后再输出到 NUL 中;
  11. exit /B %RETVAL%,退出当前批处理, /B 指定退出时的编号, 把 RETVAL 作为 退出码, 也就是 call 执行的命令的退出码;
  12. :mainEntry del /Q "%TEMP%\%~nx0.run" >NUL 2>&1,定义一个 mainEntry 标签,然后删除临时目录中的 catalina.bat.run 文件。

这段代码的作用就是调用本身,判断临时目录中的文件是否存在来避免二次回调自己。感觉绕的很晕,迷迷糊糊的,不知道设计者的意图。有没有大神可以帮忙解惑?

第二段:

  1. rem Guess CATALINA_HOME if not defined
  2. set "CURRENT_DIR=%cd%"
  3. if not "%CATALINA_HOME%" == "" goto gotHome
  4. set "CATALINA_HOME=%CURRENT_DIR%"
  5. if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
  6. cd ..
  7. set "CATALINA_HOME=%cd%"
  8. cd "%CURRENT_DIR%"
  9. :gotHome
  10. if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
  11. echo The CATALINA_HOME environment variable is not defined correctly
  12. echo This environment variable is needed to run this program
  13. goto end
  14. :okHome
  15. rem Copy CATALINA_BASE from CATALINA_HOME if not defined
  16. if not "%CATALINA_BASE%" == "" goto gotBase
  17. set "CATALINA_BASE=%CATALINA_HOME%"
  18. :gotBase
  1. set "CURRENT_DIR=%cd%",设置CURRENT_DIR为当前目录bin(假如你是在bin目录中启动);
  2. if not "%CATALINA_HOME%" == "" goto gotHome,CATALINA_HOME不是空就转向gotHome;
  3. set "CATALINA_HOME=%CURRENT_DIR%",设置CATALINA_HOME指向CURRENT_DIR的目录,也就是bin目录;
  4. if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome,如果存在%CATALINA_HOME%\bin\catalina.bat,那么就转向okHome,否则退出到父目录,(显然不存在bin\bin\catalina.bat);
  5. set "CATALINA_HOME=%cd%",设置CATALINA_HOME为当前目录(也就是Tomcat的根目录);
  6. cd "%CURRENT_DIR%",进入CURRENT_HOME指向的目录,也就是bin目录,执行gotHome标签;
  7. if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome,gotHome标签下,如果存在%CATALINA_HOME%\bin\catalina.bat,就转向okHome,因为CATALINA_HOME现在指向的目录是Tomcat的安装目录,所以\bin\catalina.bat文件是存在的,所以转向okHome标签;
  8. if not "%CATALINA_BASE%" == "" goto gotBase,如果CATALINA_BASE变量不是空,就转向gotBase标签(显然为空);
  9. set "CATALINA_BASE=%CATALINA_HOME%",设置CATALINA_BASE的值等同与CATALINA_HOME的值(现在CATALINA_BASE也指向的Tomcat的根目录了)。

这段主要是设置了两个环境变量 CATALINA_HOME 和 CATALINA_BASE。如果没有配置 CATALINA_BASE 环境变量的话, 直接引用 CATALINA_HOME 的值。

第三段:

  1. rem Ensure that neither CATALINA_HOME nor CATALINA_BASE contains a semi-colon
  2. rem as this is used as the separator in the classpath and Java provides no
  3. rem mechanism for escaping if the same character appears in the path. Check this
  4. rem by replacing all occurrences of ';' with '' and checking that neither
  5. rem CATALINA_HOME nor CATALINA_BASE have changed
  6. if "%CATALINA_HOME%" == "%CATALINA_HOME:;=%" goto homeNoSemicolon
  7. echo Using CATALINA_HOME: "%CATALINA_HOME%"
  8. echo Unable to start as CATALINA_HOME contains a semicolon (;) character
  9. goto end
  10. :homeNoSemicolon
  11. if "%CATALINA_BASE%" == "%CATALINA_BASE:;=%" goto baseNoSemicolon
  12. echo Using CATALINA_BASE: "%CATALINA_BASE%"
  13. echo Unable to start as CATALINA_BASE contains a semicolon (;) character
  14. goto end
  15. :baseNoSemicolon

这里主要是判断 CATALINA_HOME 环境变量的值 和 CATALINA_BASE 环境变量的值是否以分号为结尾,如果以分号为结尾的话,就报错退出。

第四段:

  1. rem Ensure that any user defined CLASSPATH variables are not used on startup,
  2. rem but allow them to be specified in setenv.bat, in rare case when it is needed.
  3. set CLASSPATH=
  4. rem Get standard environment variables
  5. if not exist "%CATALINA_BASE%\bin\setenv.bat" goto checkSetenvHome
  6. call "%CATALINA_BASE%\bin\setenv.bat"
  7. goto setenvDone
  8. :checkSetenvHome
  9. if exist "%CATALINA_HOME%\bin\setenv.bat" call "%CATALINA_HOME%\bin\setenv.bat"
  10. :setenvDone
  11. rem Get standard Java environment variables
  12. if exist "%CATALINA_HOME%\bin\setclasspath.bat" goto okSetclasspath
  13. echo Cannot find "%CATALINA_HOME%\bin\setclasspath.bat"
  14. echo This file is needed to run this program
  15. goto end
  16. :okSetclasspath
  17. call "%CATALINA_HOME%\bin\setclasspath.bat" %1
  18. if errorlevel 1 goto end

主要是说,如果Tomcat的bin目录下面存在setenv.bat脚本的话(setenv.bat是干什么的?上面的注释说是用户自定义的环境变量,但并不是启动时必须的定义在setenv.bat中),就执行它,通常情况下是没有的,继而又判断setclasspath.bat脚本是否存在,如果不存在的话, 直接报错,停止启动 Tomcat。如果存在的话,就去调用它,并把 第一个参数传进去。

可以进入setclasspath.bat脚本看下,主要设置了以下几个环境变量:

所以这段是设置一个临时环境变量: CLASSPATH。

第五段:

  1. rem Add on extra jar file to CLASSPATH
  2. rem Note that there are no quotes as we do not want to introduce random
  3. rem quotes into the CLASSPATH
  4. if "%CLASSPATH%" == "" goto emptyClasspath
  5. set "CLASSPATH=%CLASSPATH%;"
  6. :emptyClasspath
  7. set "CLASSPATH=%CLASSPATH%%CATALINA_HOME%\bin\bootstrap.jar"
  8. if not "%CATALINA_TMPDIR%" == "" goto gotTmpdir
  9. set "CATALINA_TMPDIR=%CATALINA_BASE%\temp"
  10. :gotTmpdir
  11. rem Add tomcat-juli.jar to classpath
  12. rem tomcat-juli.jar can be over-ridden per instance
  13. if not exist "%CATALINA_BASE%\bin\tomcat-juli.jar" goto juliClasspathHome
  14. set "CLASSPATH=%CLASSPATH%;%CATALINA_BASE%\bin\tomcat-juli.jar"
  15. goto juliClasspathDone
  16. :juliClasspathHome
  17. set "CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\bin\tomcat-juli.jar"
  18. :juliClasspathDone

这段代码主要做了三件事:

第六段:

  1. if not "%JSSE_OPTS%" == "" goto gotJsseOpts
  2. set JSSE_OPTS="-Djdk.tls.ephemeralDHKeySize=2048"
  3. :gotJsseOpts
  4. set "JAVA_OPTS=%JAVA_OPTS% %JSSE_OPTS%"
  5. rem Register custom URL handlers
  6. rem Do this here so custom URL handles (specifically 'war:...') can be used in the security policy
  7. set "JAVA_OPTS=%JAVA_OPTS% -Djava.protocol.handler.pkgs=org.apache.catalina.webresources"
  8. if not "%LOGGING_CONFIG%" == "" goto noJuliConfig
  9. set LOGGING_CONFIG=-Dnop
  10. if not exist "%CATALINA_BASE%\conf\logging.properties" goto noJuliConfig
  11. set LOGGING_CONFIG=-Djava.util.logging.config.file="%CATALINA_BASE%\conf\logging.properties"
  12. :noJuliConfig
  13. if not "%LOGGING_MANAGER%" == "" goto noJuliManager
  14. set LOGGING_MANAGER=-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
  15. :noJuliManager
  16. rem Configure JAVA 9 specific start-up parameters
  17. set "JDK_JAVA_OPTIONS=%JDK_JAVA_OPTIONS% --add-opens=java.base/java.lang=ALL-UNNAMED"
  18. set "JDK_JAVA_OPTIONS=%JDK_JAVA_OPTIONS% --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED"

主要是追加一系列的启动参数到 JAVA_OPTS 这个环境变量中。

第七段:

  1. echo Using CATALINA_BASE: "%CATALINA_BASE%"
  2. echo Using CATALINA_HOME: "%CATALINA_HOME%"
  3. echo Using CATALINA_TMPDIR: "%CATALINA_TMPDIR%"
  4. if ""%1"" == ""debug"" goto use_jdk
  5. echo Using JRE_HOME: "%JRE_HOME%"
  6. goto java_dir_displayed
  7. :use_jdk
  8. echo Using JAVA_HOME: "%JAVA_HOME%"
  9. :java_dir_displayed
  10. echo Using CLASSPATH: "%CLASSPATH%"

打印相关的环境变量信息。

第八段:

  1. set _EXECJAVA=%_RUNJAVA%
  2. set MAINCLASS=org.apache.catalina.startup.Bootstrap
  3. set ACTION=start
  4. set SECURITY_POLICY_FILE=
  5. set DEBUG_OPTS=
  6. set JPDA=

设置一些环境变量:

第九段:

  1. if not ""%1"" == ""jpda"" goto noJpda
  2. set JPDA=jpda
  3. if not "%JPDA_TRANSPORT%" == "" goto gotJpdaTransport
  4. set JPDA_TRANSPORT=dt_socket
  5. :gotJpdaTransport
  6. if not "%JPDA_ADDRESS%" == "" goto gotJpdaAddress
  7. set JPDA_ADDRESS=localhost:8000
  8. :gotJpdaAddress
  9. if not "%JPDA_SUSPEND%" == "" goto gotJpdaSuspend
  10. set JPDA_SUSPEND=n
  11. :gotJpdaSuspend
  12. if not "%JPDA_OPTS%" == "" goto gotJpdaOpts
  13. set JPDA_OPTS=-agentlib:jdwp=transport=%JPDA_TRANSPORT%,address=%JPDA_ADDRESS%,server=y,suspend=%JPDA_SUSPEND%
  14. :gotJpdaOpts
  15. shift
  16. :noJpda

好像直接从第一行跳到了最后一行,一般启动的时候没有加 jpda 参数的话, 这里会直接跳过,里面的脚本是关于 JPDA 的设置等。

第十段:

  1. if ""%1"" == ""debug"" goto doDebug
  2. if ""%1"" == ""run"" goto doRun
  3. if ""%1"" == ""start"" goto doStart
  4. if ""%1"" == ""stop"" goto doStop
  5. if ""%1"" == ""configtest"" goto doConfigTest
  6. if ""%1"" == ""version"" goto doVersion
  7. echo Usage: catalina ( commands ... )
  8. echo commands:
  9. echo debug Start Catalina in a debugger
  10. echo debug -security Debug Catalina with a security manager
  11. echo jpda start Start Catalina under JPDA debugger
  12. echo run Start Catalina in the current window
  13. echo run -security Start in the current window with security manager
  14. echo start Start Catalina in a separate window
  15. echo start -security Start in a separate window with security manager
  16. echo stop Stop Catalina
  17. echo configtest Run a basic syntax check on server.xml
  18. echo version What version of tomcat are you running?
  19. goto end

如果用 startup.bat 启动 Tomcat 的话, 这里的 "%1" 的值是 start;
如果通过 catalina.bat run 启动 Tomcat 的话,这里的 "%1" 的值是 run;

第十一段:

  1. :doDebug
  2. shift
  3. set _EXECJAVA=%_RUNJDB%
  4. set DEBUG_OPTS=-sourcepath "%CATALINA_HOME%\..\..\java"
  5. if not ""%1"" == ""-security"" goto execCmd
  6. shift
  7. echo Using Security Manager
  8. set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
  9. goto execCmd
  10. :doRun
  11. shift
  12. if not ""%1"" == ""-security"" goto execCmd
  13. shift
  14. echo Using Security Manager
  15. set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
  16. goto execCmd
  17. :doStart
  18. shift
  19. if "%TITLE%" == "" set TITLE=Tomcat
  20. set _EXECJAVA=start "%TITLE%" %_RUNJAVA%
  21. if not ""%1"" == ""-security"" goto execCmd
  22. shift
  23. echo Using Security Manager
  24. set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
  25. goto execCmd
  26. :doStop
  27. shift
  28. set ACTION=stop
  29. set CATALINA_OPTS=
  30. goto execCmd
  31. :doConfigTest
  32. shift
  33. set ACTION=configtest
  34. set CATALINA_OPTS=
  35. goto execCmd
  36. :doVersion
  37. %_EXECJAVA% -classpath "%CATALINA_HOME%\lib\catalina.jar" org.apache.catalina.util.ServerInfo
  38. goto end

分别对应前面的标签,启动主要涉及doRun和doStart。

第一个 shift 是把 start 或者 run 参数移除,然后下面 还是利用 "%1" 来取参数, 这时候, 取出来的就是参数列表中的第二个;
第二个 shift 是把第二个参数移除掉。

我们再来比较一下 start 和 run 的启动区别.

如果是 startup.bat 脚本启动的话, 会启动一个新的 cmd 窗口, 并且把 cmd 的 title 设置为 Tomcat。
如果是 catalina.bat run 启动的话, 不会新建 cmd 窗口, 也不会设置 cmd 的 title。

最后都跳到了 execCmd 标签处。

第十二段:

  1. :execCmd
  2. rem Get remaining unshifted command line arguments and save them in the
  3. set CMD_LINE_ARGS=
  4. :setArgs
  5. if ""%1""=="""" goto doneSetArgs
  6. set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
  7. shift
  8. goto setArgs
  9. :doneSetArgs

这里还是利用 "%1" 来取出启动命令之后的参数, 如果存在, 追加到 CMD_LINE_ARGS 环境变量上,并把这个参数移除。通常情况下,这里是不会有什么参数。

最后一段:

  1. rem Execute Java with the applicable properties
  2. if not "%JPDA%" == "" goto doJpda
  3. if not "%SECURITY_POLICY_FILE%" == "" goto doSecurity
  4. %_EXECJAVA% %LOGGING_CONFIG% %LOGGING_MANAGER% %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -D%ENDORSED_PROP%="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
  5. goto end
  6. :doSecurity
  7. %_EXECJAVA% %LOGGING_CONFIG% %LOGGING_MANAGER% %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -D%ENDORSED_PROP%="%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%
  8. goto end
  9. :doJpda
  10. if not "%SECURITY_POLICY_FILE%" == "" goto doSecurityJpda
  11. %_EXECJAVA% %LOGGING_CONFIG% %LOGGING_MANAGER% %JAVA_OPTS% %JPDA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -D%ENDORSED_PROP%="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
  12. goto end
  13. :doSecurityJpda
  14. %_EXECJAVA% %LOGGING_CONFIG% %LOGGING_MANAGER% %JAVA_OPTS% %JPDA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -D%ENDORSED_PROP%="%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%
  15. goto end
  16. :end

正常启动,不会有jdpa,也不会有security,所以这大段代码主要是加一些环境变量,然后带上参数去启动org.apache.catalina.startup.Bootstrap的main方法。

五、总结

启动tomcat可以双击startup.bat,也可以在命令行直接catalina.bat run,大致的流程是这样:

当然因为知识广度不够,里面还有很多信息是介绍不全的,比如jpda等,还请勿责怪,小猿需要时间慢慢成长。

参考博文:

解析Tomcat的启动脚本--catalina.bat

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注