Tomcat源码解析-1(启动脚本解析)

序言

大家新年好,好久一段时间没有更新了,感谢,感谢大家能在简书这一大家庭中陪我一起走过有关IT人的路。新的一年,继续为大家分享自己有关技术的学习路线。后续持续一段时间会为大家更新Tomcat有关的源码,欢迎大家一起探讨学习!

主题

Tocmat源码分析

①启动脚本分析
tomcat目录.png

①通过分析我们知道,Tomcat的启动是在BootStarp 的 main 函数来启动的,只是加了一个 catalina.home 的系统属性。而正常情况下启动 Tomcat 是通过运行脚本的方式,这个就涉及到建立工程时拷贝过来的 script 目录下的一堆脚本文件了。


BootStarp.png
附startup.bat
rem  @echo off 标识关闭显示命令行自身的意思
@echo off
rem Licensed to the Apache Software Foundation (ASF) under one or more
rem contributor license agreements.  See the NOTICE file distributed with
rem this work for additional information regarding copyright ownership.
rem The ASF licenses this file to You under the Apache License, Version 2.0
rem (the "License"); you may not use this file except in compliance with
rem the License.  You may obtain a copy of the License at
rem
rem     http://www.apache.org/licenses/LICENSE-2.0
rem
rem Unless required by applicable law or agreed to in writing, software
rem distributed under the License is distributed on an "AS IS" BASIS,
rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rem See the License for the specific language governing permissions and
rem limitations under the License.

rem ---------------------------------------------------------------------------
rem Start script for the CATALINA Server
rem ---------------------------------------------------------------------------

rem 下面line21--line28标识要临时修改系统变量

setlocal
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 ..


rem 设置 CATALINA_HOME 环境变量 如果操作系统中没设置,则将当前运行脚本的目录作为该环境变量的值
set "CATALINA_HOME=%cd%"
cd "%CURRENT_DIR%"
:gotHome

rem 验证 CATALINA_HOME 变量所示目录下的 catalina.bat 文件是否存在  不存在则批处理直接结束。
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

set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat"

rem Check that target executable exists
if exist "%EXECUTABLE%" goto okExec
echo Cannot find "%EXECUTABLE%"
echo This file is needed to run this program
goto end
:okExec

rem  下面一系列是标识将参数保存到 CMD_LINE_ARGS 变量中。最后第 57 行执行 catalina.bat 批处理文件,注意,该行后面跟着两个参数,第 1 个参数是 start。
rem Get remaining unshifted command line arguments and save them in the
set CMD_LINE_ARGS=
:setArgs
if ""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
shift
goto setArgs
:doneSetArgs

call "%EXECUTABLE%" start %CMD_LINE_ARGS%

:end

分析 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 if "%OS%" == "Windows_NT" setlocal
 18 rem ---------------------------------------------------------------------------
 19 rem Start/Stop Script for the CATALINA Server
 20 rem
 21 rem Environment Variable Prerequisites
 22 rem
 23 rem   Do not set the variables in this script. Instead put them into a script
 24 rem   setenv.bat in CATALINA_BASE/bin to keep your customizations separate.
 25 rem
 26 rem   CATALINA_HOME   May point at your Catalina "build" directory.
 27 rem
 28 rem   CATALINA_BASE   (Optional) Base directory for resolving dynamic portions
 29 rem                   of a Catalina installation.  If not present, resolves to
 30 rem                   the same directory that CATALINA_HOME points to.
 31 rem
 32 rem   CATALINA_OPTS   (Optional) Java runtime options used when the "start",
 33 rem                   "run" or "debug" command is executed.
 34 rem                   Include here and not in JAVA_OPTS all options, that should
 35 rem                   only be used by Tomcat itself, not by the stop process,
 36 rem                   the version command etc.
 37 rem                   Examples are heap size, GC logging, JMX ports etc.
 38 rem
 39 rem   CATALINA_TMPDIR (Optional) Directory path location of temporary directory
 40 rem                   the JVM should use (java.io.tmpdir).  Defaults to
 41 rem                   %CATALINA_BASE%\temp.
 42 rem
 43 rem   JAVA_HOME       Must point at your Java Development Kit installation.
 44 rem                   Required to run the with the "debug" argument.
 45 rem
 46 rem   JRE_HOME        Must point at your Java Runtime installation.
 47 rem                   Defaults to JAVA_HOME if empty. If JRE_HOME and JAVA_HOME
 48 rem                   are both set, JRE_HOME is used.
 49 rem
 50 rem   JAVA_OPTS       (Optional) Java runtime options used when any command
 51 rem                   is executed.
 52 rem                   Include here and not in CATALINA_OPTS all options, that
 53 rem                   should be used by Tomcat and also by the stop process,
 54 rem                   the version command etc.
 55 rem                   Most options should go into CATALINA_OPTS.
 56 rem
 57 rem   JAVA_ENDORSED_DIRS (Optional) Lists of of semi-colon separated directories
 58 rem                   containing some jars in order to allow replacement of APIs
 59 rem                   created outside of the JCP (i.e. DOM and SAX from W3C).
 60 rem                   It can also be used to update the XML parser implementation.
 61 rem                   Defaults to $CATALINA_HOME/endorsed.
 62 rem
 63 rem   JPDA_TRANSPORT  (Optional) JPDA transport used when the "jpda start"
 64 rem                   command is executed. The default is "dt_socket".
 65 rem
 66 rem   JPDA_ADDRESS    (Optional) Java runtime options used when the "jpda start"
 67 rem                   command is executed. The default is 8000.
 68 rem
 69 rem   JPDA_SUSPEND    (Optional) Java runtime options used when the "jpda start"
 70 rem                   command is executed. Specifies whether JVM should suspend
 71 rem                   execution immediately after startup. Default is "n".
 72 rem
 73 rem   JPDA_OPTS       (Optional) Java runtime options used when the "jpda start"
 74 rem                   command is executed. If used, JPDA_TRANSPORT, JPDA_ADDRESS,
 75 rem                   and JPDA_SUSPEND are ignored. Thus, all required jpda
 76 rem                   options MUST be specified. The default is:
 77 rem
 78 rem                   -agentlib:jdwp=transport=%JPDA_TRANSPORT%,
 79 rem                       address=%JPDA_ADDRESS%,server=y,suspend=%JPDA_SUSPEND%
 80 rem
 81 rem   LOGGING_CONFIG  (Optional) Override Tomcat's logging config file
 82 rem                   Example (all one line)
 83 rem                   set LOGGING_CONFIG="-Djava.util.logging.config.file=%CATALINA_BASE%\conf\logging.properties"
 84 rem
 85 rem   LOGGING_MANAGER (Optional) Override Tomcat's logging manager
 86 rem                   Example (all one line)
 87 rem                   set LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"
 88 rem
 89 rem   TITLE           (Optional) Specify the title of Tomcat window. The default
 90 rem                   TITLE is Tomcat if it's not specified.
 91 rem                   Example (all one line)
 92 rem                   set TITLE=Tomcat.Cluster#1.Server#1 [%DATE% %TIME%]
 93 rem
 94 rem
 95 rem
 96 rem $Id: catalina.bat 1344732 2012-05-31 14:08:02Z kkolinko $
 97 rem ---------------------------------------------------------------------------
 98
 99 rem Suppress Terminate batch job on CTRL+C
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"
107 rem Use provided errorlevel
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
114 rem Guess CATALINA_HOME if not defined
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
123
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
130 rem Copy CATALINA_BASE from CATALINA_HOME if not defined
131 if not "%CATALINA_BASE%" == "" goto gotBase
132 set "CATALINA_BASE=%CATALINA_HOME%"
133 :gotBase
134
135 rem Ensure that any user defined CLASSPATH variables are not used on startup,
136 rem but allow them to be specified in setenv.bat, in rare case when it is needed.
137 set CLASSPATH=
138
139 rem Get standard environment variables
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
147 rem Get standard Java environment variables
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
155
156 rem Add on extra jar file to CLASSPATH
157 rem Note that there are no quotes as we do not want to introduce random
158 rem quotes into the CLASSPATH
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
168 rem Add tomcat-juli.jar to classpath
169 rem tomcat-juli.jar can be over-ridden per instance
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
189 rem ----- Execute The Requested Command ---------------------------------------
190
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
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
298 rem Get remaining unshifted command line arguments and save them in the
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
307 rem Execute Java with the applicable properties
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%
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

1 到 99 行一大堆注释说明就不看了,100 到 110 行一堆 run 文件校验之类的一般用不到,直接跳到 111 行,接下来先删除上面可能产生的中间文件,接着按照正常流程,在上面的 startup.bat 文件中已经设置过CATALINA_HOME变量,并且校验了改变量下 bin\catalina.bat 文件已经存在(我猜作者写这一段是防止用户直接运行 catalina.bat 文件)。经过这些判断后跳转到 128 行,130 到 133 行设置CATALINA_BASE变量,中间几行注释略过,139 到 144 行如果有 setenv.bat 文件则执行一下该文件。147 到 153 行执行 setclasspath.bat 文件(该文件在同级目录下已经提供,具体脚本下面已经贴出来了,可以读一下,这里不再做分析。主要功能校验是否设置JAVA_HOME变量,没有则在控制台显示出提示信息。注意该文件中第 79 行将会设置_RUNJAVA变量,该变量的值即JAVA_HOME或JRE_HOME的 bin 目录下的 java.exe 文件)。从 156 到 224 行校验并设置一堆变量(注意第 202、203 和 204 行,最后启动分析时将会用到)。226 到 231 行将根据调用 catalina.bat 时设置的参数决定跳转,按照上一段的最后的说明,此时将会跳转到 doStart 标记行。从 265 行开始,校验和设置一些变量之后(注意第 269 行)将会跳转到 execCmd 标记行,即 297 行。297 到 305 行作用是将系统参数保存到CMD_LINE_ARGS变量中。

接下来便是谜底解开的时候,正常情况下将会执行到 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% 按照 202 行的提示_EXECJAVA一开始的值等于_RUNJAVA,但在 269 行将该值改成了start "%TITLE%" %_RUNJAVA%,这里的作用是另启动一个命令行窗口,并给窗口起名,最终还是执行 java.exe 文件。从%JAVA_OPTS%开始到%MAINCLASS%之前都是在设置运行时参数和系统属性,%MAINCLASS%即 203 行的 org.apache.catalina.startup.Bootstrap,后面是其它的命令行参数,最后一个参数%ACTION%在 204 行时被设置成 start。归结起来一句话,即执行 Bootstrap 类的 main 方法。

setclasspath.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 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  rem $Id: setclasspath.bat 1202062 2011-11-15 06:50:02Z mturk $
23  rem ---------------------------------------------------------------------------
24
25  rem Make sure prerequisite environment variables are set
26
27  rem In debug mode we need a real JDK (JAVA_HOME)
28  if ""%1"" == ""debug"" goto needJavaHome
29
30  rem Otherwise either JRE or JDK are fine
31  if not "%JRE_HOME%" == "" goto gotJreHome
32  if not "%JAVA_HOME%" == "" goto gotJavaHome
33  echo Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
34  echo At least one of these environment variable is needed to run this program
35  goto exit
36
37  :needJavaHome
38  rem Check if we have a usable JDK
39  if "%JAVA_HOME%" == "" goto noJavaHome
40  if not exist "%JAVA_HOME%\bin\java.exe" goto noJavaHome
41  if not exist "%JAVA_HOME%\bin\javaw.exe" goto noJavaHome
42  if not exist "%JAVA_HOME%\bin\jdb.exe" goto noJavaHome
43  if not exist "%JAVA_HOME%\bin\javac.exe" goto noJavaHome
44  set "JRE_HOME=%JAVA_HOME%"
45  goto okJava
46
47  :noJavaHome
48  echo The JAVA_HOME environment variable is not defined correctly.
49  echo It is needed to run this program in debug mode.
50  echo NB: JAVA_HOME should point to a JDK not a JRE.
51  goto exit
52
53  :gotJavaHome
54  rem No JRE given, use JAVA_HOME as JRE_HOME
55  set "JRE_HOME=%JAVA_HOME%"
56
57  :gotJreHome
58  rem Check if we have a usable JRE
59  if not exist "%JRE_HOME%\bin\java.exe" goto noJreHome
60  if not exist "%JRE_HOME%\bin\javaw.exe" goto noJreHome
61  goto okJava
62
63  :noJreHome
64  rem Needed at least a JRE
65  echo The JRE_HOME environment variable is not defined correctly
66  echo This environment variable is needed to run this program
67  goto exit
68
69  :okJava
70  rem Don't override the endorsed dir if the user has set it previously
71  if not "%JAVA_ENDORSED_DIRS%" == "" goto gotEndorseddir
72  rem Set the default -Djava.endorsed.dirs argument
73  set "JAVA_ENDORSED_DIRS=%CATALINA_HOME%\endorsed"
74  :gotEndorseddir
75
76  rem Set standard command for invoking Java.
77  rem Note that NT requires a window name argument when using start.
78  rem Also note the quoting as JAVA_HOME may contain spaces.
79  set _RUNJAVA="%JRE_HOME%\bin\java"
80  set _RUNJDB="%JAVA_HOME%\bin\jdb"
81
82  goto end
83
84  :exit
85  exit /b 1
86
87  :end
88  exit /b 0

以上即 tomcat 的启动脚本中的总体流程,只是讲了默认的脚本运行方式,但在脚本阅读中可以看到能根据 catalina.bat 里可以根据不同参数以不同方式运行 tomcat :

debug             Start Catalina in a debugger
debug -security   Debug Catalina with a security manager
jpda start        Start Catalina under JPDA debugger
run               Start Catalina in the current window
run -security     Start in the current window with security manager
start             Start Catalina in a separate window
start -security   Start in a separate window with security manager
stop              Stop Catalina
configtest        Run a basic syntax check on server.xml

只要能看懂基本 dos 命令,这几个脚本的阅读其实很简单,其他 bat 文件可以大体浏览一下,会发现转了半天,最终都会执行 catalina.bat 文件,而 catalina.bat 文件里最终会执行 Bootstrap 文件的 main 方法,不同的是调用 main 方法时会各自添加不同的入参而已。bat 文件用于 windows 操作系统下启动 tomcat,而 sh 文件则用于 unix 环境下的启动,原理是一致的。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,839评论 6 482
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,543评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 153,116评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,371评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,384评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,111评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,416评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,053评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,558评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,007评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,117评论 1 334
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,756评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,324评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,315评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,539评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,578评论 2 355
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,877评论 2 345

推荐阅读更多精彩内容