一、前言
一般启动tomcat都是找到bin目录下的startup.bat(windows)下或者startup.sh(linux下),所以要研究tomcat的启动,startup脚本是避不开的。那么下面就来看一看windows下面的startup.bat。
二、bat文件
要看懂startup.bat,首先得对bat文件有个基本的了解。
bat文件是dos下的批处理文件。批处理文件是无格式的文本文件,这个文件的每一行都是一条DOS命令。它的文件扩展名为 .bat 或 .cmd。在命令提示下键入批处理文件的名称,或者双击该批处理文件,系统就会调用cmd.exe按照该文件中各个命令出现的顺序来逐个运行它们。使用批处理文件(也被称为批处理程序或脚本),可以简化日常或重复性任务。
批处理简单语法:
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窗口。
可以参考一下这篇博文:
三、startup.bat
用编辑器打开startup.bat文件,内容如下:
1 @echo off2 rem Licensed to the Apache Software Foundation (ASF) under one or more3 rem contributor license agreements. See the NOTICE file distributed with4 rem this work foradditional 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 incompliance with7 rem the License. You may obtain a copy of the License at8 rem9 rem http://www.apache.org/licenses/LICENSE-2.0
10 rem11 rem Unless required by applicable law or agreed to inwriting, software12 rem distributed under the License is distributed on an "AS IS"BASIS,13 rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14 rem See the License forthe specific language governing permissions and15 rem limitations under the License.16
17 rem ---------------------------------------------------------------------------
18 rem Start script forthe CATALINA Server19 rem ---------------------------------------------------------------------------
20
21 setlocal22
23 rem Guess CATALINA_HOME ifnot defined24 set "CURRENT_DIR=%cd%"
25 if not "%CATALINA_HOME%" == "" gotogotHome26 set "CATALINA_HOME=%CURRENT_DIR%"
27 if exist "%CATALINA_HOME%\bin\catalina.bat" gotookHome28 cd ..29 set "CATALINA_HOME=%cd%"
30 cd "%CURRENT_DIR%"
31 :gotHome32 if exist "%CATALINA_HOME%\bin\catalina.bat" gotookHome33 echo The CATALINA_HOME environment variable isnot defined correctly34 echo This environment variable is needed to run thisprogram35 gotoend36 :okHome37
38 set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat"
39
40 rem Check that target executable exists41 if exist "%EXECUTABLE%" gotookExec42 echo Cannot find "%EXECUTABLE%"
43 echo This file is needed to run thisprogram44 gotoend45 :okExec46
47 rem Get remaining unshifted command line arguments and save them inthe48 set CMD_LINE_ARGS=
49 :setArgs50 if ""%1""=="""" gotodoneSetArgs51 set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
52 shift53 gotosetArgs54 :doneSetArgs55
56 call "%EXECUTABLE%" start %CMD_LINE_ARGS%
57
58 :end
@echo off,此语句常用于开头,表示不显示所有的命令行信息,包括此句。
然后是一大段rem后面的内容,是注释,对软件的License做了说明,英文不太好,就不翻译了。
setlocal,启动批处理文件中环境变量的本地化,本地化将持续到出现匹配的endlocal命令或者到达批处理文件结尾为止。
下面将批处理文件拆成几段看:
第一段:判断CATALINA_HOME是否定义
1 setlocal2
3 rem Guess CATALINA_HOME ifnot defined4 set "CURRENT_DIR=%cd%"
5 if not "%CATALINA_HOME%" == "" gotogotHome6 set "CATALINA_HOME=%CURRENT_DIR%"
7 if exist "%CATALINA_HOME%\bin\catalina.bat" gotookHome8 cd ..9 set "CATALINA_HOME=%cd%"
10 cd "%CURRENT_DIR%"
设置变量CURRENT_DIR为当前目录,一般打开的就是tomcat的bin目录,./apache-tomcat-x.x.xx/bin;
判断系统变量CATALINA_HOME是不是空串(一般安装tomcat时,我们都会配置CATALINA_HOME环境变量,不知有没有印象呢),如果是就跳转到gotHome标签执行,假设CATALINA_HOME环境变量没有定义,就继续往下;
设置CATALINA_HOME为CURRENT_DIR指向的目录,也就是bin目录(./apache-tomcat-x.x.xx/bin);
判断CATALINA_HOME\bin\catalina.bat文件是否存在,也就是 ./apache-tomcat-x.x.xx/bin/bin/catalina.bat,如果存在就转向okHome标签,显然是不存在的,所以不会跳转到okHome,继续往下;
不存在的话,CATALINA_HOME取上级目录的值,也就是(./apache-tomcat-x.x.xx/);
设置CATALINA_HOME指向当前目录,也就是Tomcat的根目录;
进入 CURRENT_DIR(./apache-tomcat-x.x.xx/bin);
第二段:执行gotHome标签
1 :gotHome2 if exist "%CATALINA_HOME%\bin\catalina.bat" gotookHome3 echo The CATALINA_HOME environment variable isnot defined correctly4 echo This environment variable is needed to run thisprogram5 goto end
通过上面的设置,CATALINA_HOME的值已经是: ./apache-tomcat-x.x.xx/,这时判断./apache-tomcat-x.x.xx/catalina.bat 脚本是否存在,如果不存在,则输出一些信息结束,一般这时是可以找到的,所以跳转到okHome标签处;
第三段:执行okHome标签
1 :okHome2
3 set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat"
设置EXECUTABLE变量指向为catalina.bat脚本;
第四段:再次检查catalina.bat是否存在
1 rem Check that target executable exists2 if exist "%EXECUTABLE%" gotookExec3 echo Cannot find "%EXECUTABLE%" 4 echo This file is needed to run thisprogram5 goto end
双重保险继续判断下EXECUTABLE指向的catalina.bat文件是否存在,不存在就输出提示信息并且结束,存在就跳转到okExec标签处:
第五段:
1 :okExec2
3 rem Get remaining unshifted command line arguments and save them inthe4 set CMD_LINE_ARGS=
5 :setArgs6 if ""%1""=="""" gotodoneSetArgs7 set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
8 shift9 gotosetArgs10 :doneSetArgs11
12 call "%EXECUTABLE%" start %CMD_LINE_ARGS%
set CMD_LINE_ARGS=表示清空变量CMD_LINE_ARGS;
执行setArgs标签;
第一个变量(%1%)为空字符串,那么就跳转到doneSetArgs标签,一般情况下,直接运行startup.bat,是没有传递任何参数,所以是跳转到doneSetArgs标签,由此也可以猜想出,如果不是使用双击执行的话,使用命令行启动startup.bat那么是可以传递参数的;
跳转到doneSetArgs,调用EXECUTABLE指向的文件,也就是catalina.bat文件,同时传递start参数,因为CMD_LINE_ARGS为空,所以只传递了一个start参数
**最后:**end标签
1 :end
判断失败跳转的标签,是参数不正确的时候的结束标志。
四、catalina.bat
如果startup.bat执行正确,就等同于运行catalina.bat start,进入了catalina.bat脚本中,将脚本前面的一大段注释删掉了,如下:
1 setlocal2
3 rem Suppress Terminate batch job on CTRL+C4 if not ""%1"" == ""run"" gotomainEntry5 if "%TEMP%" == "" gotomainEntry6 if exist "%TEMP%\%~nx0.run" gotomainEntry7 echo Y>"%TEMP%\%~nx0.run"
8 if not exist "%TEMP%\%~nx0.run" gotomainEntry9 echo Y>"%TEMP%\%~nx0.Y"
10 call "%~f0" %*
11 rem Use provided errorlevel12 set RETVAL=%ERRORLEVEL%
13 del /Q "%TEMP%\%~nx0.Y" >NUL 2>&1
14 exit /B %RETVAL%
15 :mainEntry16 del /Q "%TEMP%\%~nx0.run" >NUL 2>&1
17
18 rem Guess CATALINA_HOME ifnot defined19 set "CURRENT_DIR=%cd%"
20 if not "%CATALINA_HOME%" == "" gotogotHome21 set "CATALINA_HOME=%CURRENT_DIR%"
22 if exist "%CATALINA_HOME%\bin\catalina.bat" gotookHome23 cd ..24 set "CATALINA_HOME=%cd%"
25 cd "%CURRENT_DIR%"
26 :gotHome27
28 if exist "%CATALINA_HOME%\bin\catalina.bat" gotookHome29 echo The CATALINA_HOME environment variable isnot defined correctly30 echo This environment variable is needed to run thisprogram31 gotoend32 :okHome33
34 rem Copy CATALINA_BASE from CATALINA_HOME ifnot defined35 if not "%CATALINA_BASE%" == "" gotogotBase36 set "CATALINA_BASE=%CATALINA_HOME%"
37 :gotBase38
39 rem Ensure that neither CATALINA_HOME nor CATALINA_BASE contains a semi-colon40 rem as this is used as the separator inthe classpath and Java provides no41 rem mechanism for escaping if the same character appears in the path. Check this
42 rem by replacing all occurrences of ';' with ''and checking that neither43 rem CATALINA_HOME nor CATALINA_BASE have changed44 if "%CATALINA_HOME%" == "%CATALINA_HOME:;=%" gotohomeNoSemicolon45 echo Using CATALINA_HOME: "%CATALINA_HOME%"
46 echo Unable to start asCATALINA_HOME contains a semicolon (;) character47 gotoend48 :homeNoSemicolon49
50 if "%CATALINA_BASE%" == "%CATALINA_BASE:;=%" gotobaseNoSemicolon51 echo Using CATALINA_BASE: "%CATALINA_BASE%"
52 echo Unable to start asCATALINA_BASE contains a semicolon (;) character53 gotoend54 :baseNoSemicolon55
56 rem Ensure that any user defined CLASSPATH variables are not used on startup,57 rem but allow them to be specified in setenv.bat, in rare case when it isneeded.58 set CLASSPATH=
59
60 rem Get standard environment variables61 if not exist "%CATALINA_BASE%\bin\setenv.bat" gotocheckSetenvHome62 call "%CATALINA_BASE%\bin\setenv.bat"
63 gotosetenvDone64 :checkSetenvHome65 if exist "%CATALINA_HOME%\bin\setenv.bat" call "%CATALINA_HOME%\bin\setenv.bat"
66 :setenvDone67
68 rem Get standard Java environment variables69 if exist "%CATALINA_HOME%\bin\setclasspath.bat" gotookSetclasspath70 echo Cannot find "%CATALINA_HOME%\bin\setclasspath.bat"
71 echo This file is needed to run thisprogram72 gotoend73 :okSetclasspath74 call "%CATALINA_HOME%\bin\setclasspath.bat" %1
75 if errorlevel 1 gotoend76
77 rem Add on extra jar file to CLASSPATH78 rem Note that there are no quotes as we donot want to introduce random79 rem quotes into the CLASSPATH80 if "%CLASSPATH%" == "" gotoemptyClasspath81 set "CLASSPATH=%CLASSPATH%;"
82 :emptyClasspath83 set "CLASSPATH=%CLASSPATH%%CATALINA_HOME%\bin\bootstrap.jar"
84
85 if not "%CATALINA_TMPDIR%" == "" gotogotTmpdir86 set "CATALINA_TMPDIR=%CATALINA_BASE%\temp"
87 :gotTmpdir88
89 rem Add tomcat-juli.jar to classpath90 rem tomcat-juli.jar can be over-ridden per instance91 if not exist "%CATALINA_BASE%\bin\tomcat-juli.jar" gotojuliClasspathHome92 set "CLASSPATH=%CLASSPATH%;%CATALINA_BASE%\bin\tomcat-juli.jar"
93 gotojuliClasspathDone94 :juliClasspathHome95 set "CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\bin\tomcat-juli.jar"
96 :juliClasspathDone97
98 if not "%JSSE_OPTS%" == "" gotogotJsseOpts99 set JSSE_OPTS="-Djdk.tls.ephemeralDHKeySize=2048"
100 :gotJsseOpts101 set "JAVA_OPTS=%JAVA_OPTS% %JSSE_OPTS%"
102
103 rem Register custom URL handlers104 rem Do this here so custom URL handles (specifically 'war:...') can be used inthe security policy105 set "JAVA_OPTS=%JAVA_OPTS% -Djava.protocol.handler.pkgs=org.apache.catalina.webresources"
106
107 if not "%LOGGING_CONFIG%" == "" gotonoJuliConfig108 set LOGGING_CONFIG=-Dnop109 if not exist "%CATALINA_BASE%\conf\logging.properties" gotonoJuliConfig110 set LOGGING_CONFIG=-Djava.util.logging.config.file="%CATALINA_BASE%\conf\logging.properties"
111 :noJuliConfig112
113 if not "%LOGGING_MANAGER%" == "" gotonoJuliManager114 set LOGGING_MANAGER=-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager115 :noJuliManager116
117 rem Configure JAVA 9 specific start-up parameters118 set "JDK_JAVA_OPTIONS=%JDK_JAVA_OPTIONS% --add-opens=java.base/java.lang=ALL-UNNAMED"
119 set "JDK_JAVA_OPTIONS=%JDK_JAVA_OPTIONS% --add-opens=java.base/java.io=ALL-UNNAMED"
120 set "JDK_JAVA_OPTIONS=%JDK_JAVA_OPTIONS% --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED"
121
122 rem Java 9no longer supports the java.endorsed.dirs123 rem system property. Only try to use it if
124 rem JAVA_ENDORSED_DIRS was explicitly set
125 rem or CATALINA_HOME/endorsed exists.126 set ENDORSED_PROP=ignore.endorsed.dirs127 if "%JAVA_ENDORSED_DIRS%" == "" gotonoEndorsedVar128 set ENDORSED_PROP=java.endorsed.dirs129 gotodoneEndorsed130 :noEndorsedVar131 if not exist "%CATALINA_HOME%\endorsed" gotodoneEndorsed132 set ENDORSED_PROP=java.endorsed.dirs133 :doneEndorsed134
135 rem ----- Execute The Requested Command ---------------------------------------
136
137 echo Using CATALINA_BASE: "%CATALINA_BASE%"
138 echo Using CATALINA_HOME: "%CATALINA_HOME%"
139 echo Using CATALINA_TMPDIR: "%CATALINA_TMPDIR%"
140 if ""%1"" == ""debug"" gotouse_jdk141 echo Using JRE_HOME: "%JRE_HOME%"
142 gotojava_dir_displayed143 :use_jdk144 echo Using JAVA_HOME: "%JAVA_HOME%"
145 :java_dir_displayed146 echo Using CLASSPATH: "%CLASSPATH%"
147
148 set _EXECJAVA=%_RUNJAVA%
149 set MAINCLASS=org.apache.catalina.startup.Bootstrap150 set ACTION=start151 set SECURITY_POLICY_FILE=
152 set DEBUG_OPTS=
153 set JPDA=
154
155 if not ""%1"" == ""jpda"" gotonoJpda156 set JPDA=jpda157 if not "%JPDA_TRANSPORT%" == "" gotogotJpdaTransport158 set JPDA_TRANSPORT=dt_socket159 :gotJpdaTransport160 if not "%JPDA_ADDRESS%" == "" gotogotJpdaAddress161 set JPDA_ADDRESS=localhost:8000
162 :gotJpdaAddress163 if not "%JPDA_SUSPEND%" == "" gotogotJpdaSuspend164 set JPDA_SUSPEND=n165 :gotJpdaSuspend166 if not "%JPDA_OPTS%" == "" gotogotJpdaOpts167 set JPDA_OPTS=-agentlib:jdwp=transport=%JPDA_TRANSPORT%,address=%JPDA_ADDRESS%,server=y,suspend=%JPDA_SUSPEND%
168 :gotJpdaOpts169 shift170 :noJpda171
172 if ""%1"" == ""debug"" gotodoDebug173 if ""%1"" == ""run"" gotodoRun174 if ""%1"" == ""start"" gotodoStart175 if ""%1"" == ""stop"" gotodoStop176 if ""%1"" == ""configtest"" gotodoConfigTest177 if ""%1"" == ""version"" gotodoVersion178
179 echo Usage: catalina ( commands ... )180 echo commands:181 echo debug Start Catalina ina debugger182 echo debug -security Debug Catalina with a security manager183 echo jpda start Start Catalina under JPDA debugger184 echo run Start Catalina inthe current window185 echo run -security Start inthe current window with security manager186 echo start Start Catalina ina separate window187 echo start -security Start ina separate window with security manager188 echo stop Stop Catalina189 echo configtest Run a basic syntax check on server.xml190 echo version What version of tomcat are you running?
191 gotoend192
193 :doDebug194 shift195 set _EXECJAVA=%_RUNJDB%
196 set DEBUG_OPTS=-sourcepath "%CATALINA_HOME%\..\..\java"
197 if not ""%1"" == ""-security"" gotoexecCmd198 shift199 echo Using Security Manager200 set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
201 gotoexecCmd202
203 :doRun204 shift205 if not ""%1"" == ""-security"" gotoexecCmd206 shift207 echo Using Security Manager208 set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
209 gotoexecCmd210
211 :doStart212 shift213 if "%TITLE%" == "" set TITLE=Tomcat214 set _EXECJAVA=start "%TITLE%" %_RUNJAVA%
215 if not ""%1"" == ""-security"" gotoexecCmd216 shift217 echo Using Security Manager218 set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
219 gotoexecCmd220
221 :doStop222 shift223 set ACTION=stop224 set CATALINA_OPTS=
225 gotoexecCmd226
227 :doConfigTest228 shift229 set ACTION=configtest230 set CATALINA_OPTS=
231 gotoexecCmd232
233 :doVersion234 %_EXECJAVA% -classpath "%CATALINA_HOME%\lib\catalina.jar"org.apache.catalina.util.ServerInfo235 gotoend236
237
238 :execCmd239 rem Get remaining unshifted command line arguments and save them inthe240 set CMD_LINE_ARGS=
241 :setArgs242 if ""%1""=="""" gotodoneSetArgs243 set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
244 shift245 gotosetArgs246 :doneSetArgs247
248 rem Execute Java with the applicable properties249 if not "%JPDA%" == "" gotodoJpda250 if not "%SECURITY_POLICY_FILE%" == "" gotodoSecurity251 %_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%
252 gotoend253 :doSecurity254 %_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%
255 gotoend256 :doJpda257 if not "%SECURITY_POLICY_FILE%" == "" gotodoSecurityJpda258 %_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%
259 gotoend260 :doSecurityJpda261 %_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%
262 gotoend263
264 :end
第一段:
1 rem Suppress Terminate batch job on CTRL+C2 if not ""%1"" == ""run""goto mainEntry3 if "%TEMP%" == ""goto mainEntry4 if exist "%TEMP%\%~nx0.run"goto mainEntry5 echo Y>"%TEMP%\%~nx0.run"
6 if not exist "%TEMP%\%~nx0.run"goto mainEntry7 echo Y>"%TEMP%\%~nx0.Y"
8 call "%~f0" %*
9 rem Use provided errorlevel10 set RETVAL=%ERRORLEVEL%
11 del /Q "%TEMP%\%~nx0.Y" >NUL 2>&1
12 exit /B %RETVAL%
13 :mainEntry14 del /Q "%TEMP%\%~nx0.run" >NUL 2>&1
首先注释是说,禁止使用 CTRL+C 来终止批处理任务;
if not ""%1"" == ""run"" goto mainEntry,第一个参数不是run,就转向mainEntry标签,通过调用startup.bat启动的第一个参数应该是start;
if "%TEMP%" == "" goto mainEntry,%TEMP%是系统的环境变量值,通常装完windows系统的话, 系统会自动配置上这个环境变量,一般是C:\Users\用户名\AppData\Local\Temp(AppData是一个隐藏文件夹)。这句是说,如果%TEMP%是空串,就转向mainEntry标签;
if exist "%TEMP%\%~nx0.run" goto mainEntry, 在批处理中,%1 表示的是程序之后的第一个参数,%0表示这个可执行程序的名称, %~nx0 的话就是程序的名称+扩展名,在这里就是catalina.bat(可以写一个脚本测试下);
echo Y>"%TEMP%\%~nx0.run",写入字符Y到%TEMP%\catalina.bat.run 文件中;
if not exist "%TEMP%\%~nx0.run" goto mainEntry,又判断了一下 %TEMP%\catalina.bat.run 文件是否存在;
echo Y>"%TEMP%\%~nx0.Y",写入Y到%TEMP%\catalina.bat.Y,如果文件不存在, 则新建一个;
call "%~f0" %*
set RETVAL=%ERRORLEVEL%,Windows中和linux一样,命令执行完之后都有自己的退出码,%ERRORLEVEL%就是取的上面的call 命令的退出码,赋值给一个变量RETVAL;
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 中;
exit /B %RETVAL%,退出当前批处理, /B 指定退出时的编号, 把 RETVAL 作为 退出码, 也就是 call 执行的命令的退出码;
:mainEntry del /Q “%TEMP%\%~nx0.run” >NUL 2>&1,定义一个 mainEntry 标签,然后删除临时目录中的 catalina.bat.run 文件。
这段代码的作用就是调用本身,判断临时目录中的文件是否存在来避免二次回调自己
第二段:
1 rem Guess CATALINA_HOME ifnot defined2 set "CURRENT_DIR=%cd%"
3 if not "%CATALINA_HOME%" == ""goto gotHome4 set "CATALINA_HOME=%CURRENT_DIR%"
5 if exist "%CATALINA_HOME%\bin\catalina.bat"goto okHome6 cd ..7 set "CATALINA_HOME=%cd%"
8 cd "%CURRENT_DIR%"
9 :gotHome10
11 if exist "%CATALINA_HOME%\bin\catalina.bat"goto okHome12 echoThe CATALINA_HOME environment variable is not defined correctly13 echoThis environment variable is needed to run this program14 goto end15 :okHome16
17 rem Copy CATALINA_BASE from CATALINA_HOME ifnot defined18 if not "%CATALINA_BASE%" == ""goto gotBase19 set "CATALINA_BASE=%CATALINA_HOME%"
20 :gotBase
set "CURRENT_DIR=%cd%",设置CURRENT_DIR为当前目录bin(假如你是在bin目录中启动);
if not "%CATALINA_HOME%" == "" goto gotHome,CATALINA_HOME不是空就转向gotHome;
set "CATALINA_HOME=%CURRENT_DIR%",设置CATALINA_HOME指向CURRENT_DIR的目录,也就是bin目录;
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome,如果存在%CATALINA_HOME%\bin\catalina.bat,那么就转向okHome,否则退出到父目录,(显然不存在bin\bin\catalina.bat);
set "CATALINA_HOME=%cd%",设置CATALINA_HOME为当前目录(也就是Tomcat的根目录);
cd "%CURRENT_DIR%",进入CURRENT_HOME指向的目录,也就是bin目录,执行gotHome标签;
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome,gotHome标签下,如果存在%CATALINA_HOME%\bin\catalina.bat,就转向okHome,因为CATALINA_HOME现在指向的目录是Tomcat的安装目录,所以\bin\catalina.bat文件是存在的,所以转向okHome标签;
if not "%CATALINA_BASE%" == "" goto gotBase,如果CATALINA_BASE变量不是空,就转向gotBase标签(显然为空);
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-colon2 rem as this is used as the separator inthe classpath and Java provides no3 rem mechanism for escaping if the same character appears inthe path. Check this4 rem by replacing all occurrences of ';' with ''and checking that neither5 rem CATALINA_HOME nor CATALINA_BASE have changed6 if "%CATALINA_HOME%" == "%CATALINA_HOME:;=%"goto homeNoSemicolon7 echo Using CATALINA_HOME: "%CATALINA_HOME%"
8 echoUnable to start as CATALINA_HOME contains a semicolon (;) character9 goto end10 :homeNoSemicolon11
12 if "%CATALINA_BASE%" == "%CATALINA_BASE:;=%"goto baseNoSemicolon13 echo Using CATALINA_BASE: "%CATALINA_BASE%"
14 echoUnable to start as CATALINA_BASE contains a semicolon (;) character15 goto end16 :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 casewhen it is needed.3 set CLASSPATH=
4
5 rem Get standard environment variables6 if not exist "%CATALINA_BASE%\bin\setenv.bat"goto checkSetenvHome7 call "%CATALINA_BASE%\bin\setenv.bat"
8 goto setenvDone9 :checkSetenvHome10 if exist "%CATALINA_HOME%\bin\setenv.bat" call "%CATALINA_HOME%\bin\setenv.bat"
11 :setenvDone12
13 rem Get standard Java environment variables14 if exist "%CATALINA_HOME%\bin\setclasspath.bat"goto okSetclasspath15 echo Cannot find "%CATALINA_HOME%\bin\setclasspath.bat"
16 echo This fileis needed to run this program17 goto end18 :okSetclasspath19 call "%CATALINA_HOME%\bin\setclasspath.bat" %1
20 if errorlevel 1 goto end
主要是说,如果Tomcat的bin目录下面存在setenv.bat脚本的话(setenv.bat是干什么的?上面的注释说是用户自定义的环境变量,但并不是启动时必须的定义在setenv.bat中),就执行它,通常情况下是没有的,继而又判断setclasspath.bat脚本是否存在,如果不存在的话, 直接报错,停止启动 Tomcat。如果存在的话,就去调用它,并把 第一个参数传进去。
可以进入setclasspath.bat脚本看下,主要设置了以下几个环境变量:
JAVA_HOME
JRE_HOME
JAVA_ENDORSED_DIRS = %CATALINA_HOME%\endorsed
_RUNJAVA = %JRE_HOME%\bin\java.exe
_RUNJDB = %JAVA_HOME%\bin\jdb.exe
所以这段是设置一个临时环境变量: CLASSPATH。
第五段:
1 rem Add on extra jar fileto CLASSPATH2 rem Note that there are no quotes as we donot want to introduce random3 rem quotes into the CLASSPATH4 if "%CLASSPATH%" == ""goto emptyClasspath5 set "CLASSPATH=%CLASSPATH%;"
6 :emptyClasspath7 set "CLASSPATH=%CLASSPATH%%CATALINA_HOME%\bin\bootstrap.jar"
8
9 if not "%CATALINA_TMPDIR%" == ""goto gotTmpdir10 set "CATALINA_TMPDIR=%CATALINA_BASE%\temp"
11 :gotTmpdir12
13 rem Add tomcat-juli.jar to classpath14 rem tomcat-juli.jar can be over-ridden per instance15 if not exist "%CATALINA_BASE%\bin\tomcat-juli.jar"goto juliClasspathHome16 set "CLASSPATH=%CLASSPATH%;%CATALINA_BASE%\bin\tomcat-juli.jar"
17 goto juliClasspathDone18 :juliClasspathHome19 set "CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\bin\tomcat-juli.jar"
20 :juliClasspathDone
这段代码主要做了三件事:
把 Tomcat bin 目录下的 bootstrap.jar 加入到环境变量中;
设置 CATALINA_TMPDIR 环境变量的值为 Tomcat 目录下的 temp 目录;
把 Tomcat bin 目录下的 tomcat-juli.jar 加入到环境变量中;
第六段:
1 if not "%JSSE_OPTS%" == ""goto gotJsseOpts2 set JSSE_OPTS="-Djdk.tls.ephemeralDHKeySize=2048"
3 :gotJsseOpts4 set "JAVA_OPTS=%JAVA_OPTS% %JSSE_OPTS%"
5
6 rem Register custom URL handlers7 rem Do this here so custom URL handles (specifically 'war:...') can be used inthe security policy8 set "JAVA_OPTS=%JAVA_OPTS% -Djava.protocol.handler.pkgs=org.apache.catalina.webresources"
9
10 if not "%LOGGING_CONFIG%" == ""goto noJuliConfig11 set LOGGING_CONFIG=-Dnop12 if not exist "%CATALINA_BASE%\conf\logging.properties"goto noJuliConfig13 set LOGGING_CONFIG=-Djava.util.logging.config.file="%CATALINA_BASE%\conf\logging.properties"
14 :noJuliConfig15
16 if not "%LOGGING_MANAGER%" == ""goto noJuliManager17 set LOGGING_MANAGER=-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager18 :noJuliManager19
20 rem Configure JAVA 9 specific start-up parameters21 set "JDK_JAVA_OPTIONS=%JDK_JAVA_OPTIONS% --add-opens=java.base/java.lang=ALL-UNNAMED"
22 set "JDK_JAVA_OPTIONS=%JDK_JAVA_OPTIONS% --add-opens=java.base/java.io=ALL-UNNAMED"
23 set "JDK_JAVA_OPTIONS=%JDK_JAVA_OPTIONS% --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED"
主要是追加一系列的启动参数到 JAVA_OPTS 这个环境变量中。
1 rem Java 9no longer supports the java.endorsed.dirs2 rem system property. Only try to use it if
3 rem JAVA_ENDORSED_DIRS was explicitly set4 rem or CATALINA_HOME/endorsed exists.5 set ENDORSED_PROP=ignore.endorsed.dirs6 if "%JAVA_ENDORSED_DIRS%" == ""goto noEndorsedVar7 set ENDORSED_PROP=java.endorsed.dirs8 goto doneEndorsed9 :noEndorsedVar10 if not exist "%CATALINA_HOME%\endorsed"goto doneEndorsed11 set ENDORSED_PROP=java.endorsed.dirs12 :doneEndorsed
第七段:
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_jdk5 echo Using JRE_HOME: "%JRE_HOME%"
6 goto java_dir_displayed7 :use_jdk8 echo Using JAVA_HOME: "%JAVA_HOME%"
9 :java_dir_displayed10 echo Using CLASSPATH: "%CLASSPATH%"
打印相关的环境变量信息。
第八段:
1 set _EXECJAVA=%_RUNJAVA%
2 set MAINCLASS=org.apache.catalina.startup.Bootstrap3 set ACTION=start4 set SECURITY_POLICY_FILE=
5 set DEBUG_OPTS=
6 set JPDA=
设置一些环境变量:
_RUNJAVA : %JRE_HOME%\bin\java.exe;
MAINCLASS : 指定了 Tomcat 的启动类;
ACTION : 动作, 就是启动;
SECURITY_POLICY_FILE : 安全策略文件,如果启动的时候加上了 -security 参数的话,下面会对这个参数指定到 Tomcat 的 conf 目录下的 catalina.policy 文件;
JPDA : 还不清楚 。
第九段:
1 if not ""%1"" == ""jpda""goto noJpda2 set JPDA=jpda3 if not "%JPDA_TRANSPORT%" == ""goto gotJpdaTransport4 set JPDA_TRANSPORT=dt_socket5 :gotJpdaTransport6 if not "%JPDA_ADDRESS%" == ""goto gotJpdaAddress7 set JPDA_ADDRESS=localhost:8000
8 :gotJpdaAddress9 if not "%JPDA_SUSPEND%" == ""goto gotJpdaSuspend10 set JPDA_SUSPEND=n11 :gotJpdaSuspend12 if not "%JPDA_OPTS%" == ""goto gotJpdaOpts13 set JPDA_OPTS=-agentlib:jdwp=transport=%JPDA_TRANSPORT%,address=%JPDA_ADDRESS%,server=y,suspend=%JPDA_SUSPEND%
14 :gotJpdaOpts15 shift
16 :noJpda
好像直接从第一行跳到了最后一行,一般启动的时候没有加 jpda 参数的话, 这里会直接跳过,里面的脚本是关于 JPDA 的设置等。
第十段:
1 if ""%1"" == ""debug""goto doDebug2 if ""%1"" == ""run""goto doRun3 if ""%1"" == ""start""goto doStart4 if ""%1"" == ""stop""goto doStop5 if ""%1"" == ""configtest""goto doConfigTest6 if ""%1"" == ""version""goto doVersion7
8 echoUsage: catalina ( commands ... )9 echocommands:10 echo debug Start Catalina ina debugger11 echo debug -security Debug Catalina with a security manager12 echojpda start Start Catalina under JPDA debugger13 echo run Start Catalina inthe current window14 echo run -security Start inthe current window with security manager15 echo start Start Catalina ina separate window16 echo start -security Start ina separate window with security manager17 echostop Stop Catalina18 echoconfigtest Run a basic syntax check on server.xml19 echo version What version of tomcat are you running?
20 goto end
如果用 startup.bat 启动 Tomcat 的话, 这里的 “%1” 的值是 start;
如果通过 catalina.bat run 启动 Tomcat 的话,这里的 “%1” 的值是 run;
第十一段:
1 :doDebug2 shift
3 set _EXECJAVA=%_RUNJDB%
4 set DEBUG_OPTS=-sourcepath "%CATALINA_HOME%\..\..\java"
5 if not ""%1"" == ""-security""goto execCmd6 shift
7 echoUsing Security Manager8 set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
9 goto execCmd10
11 :doRun12 shift
13 if not ""%1"" == ""-security""goto execCmd14 shift
15 echoUsing Security Manager16 set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
17 goto execCmd18
19 :doStart20 shift
21 if "%TITLE%" == "" set TITLE=Tomcat22 set _EXECJAVA=start "%TITLE%" %_RUNJAVA%
23 if not ""%1"" == ""-security""goto execCmd24 shift
25 echoUsing Security Manager26 set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
27 goto execCmd28
29 :doStop30 shift
31 set ACTION=stop32 set CATALINA_OPTS=
33 goto execCmd34
35 :doConfigTest36 shift
37 set ACTION=configtest38 set CATALINA_OPTS=
39 goto execCmd40
41 :doVersion42 %_EXECJAVA% -classpath "%CATALINA_HOME%\lib\catalina.jar"org.apache.catalina.util.ServerInfo43 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 :execCmd2 rem Get remaining unshifted command line arguments and save them inthe3 set CMD_LINE_ARGS=
4 :setArgs5 if ""%1""==""""goto doneSetArgs6 set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
7 shift
8 goto setArgs9 :doneSetArgs
这里还是利用 “%1” 来取出启动命令之后的参数, 如果存在, 追加到 CMD_LINE_ARGS 环境变量上,并把这个参数移除。通常情况下,这里是不会有什么参数。
最后一段:
1 rem Execute Java with the applicable properties2 if not "%JPDA%" == ""goto doJpda3 if not "%SECURITY_POLICY_FILE%" == ""goto doSecurity4 %_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 end6 :doSecurity7 %_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 end9 :doJpda10 if not "%SECURITY_POLICY_FILE%" == ""goto doSecurityJpda11 %_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 end13 :doSecurityJpda14 %_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 end16
17 :end
正常启动,不会有jdpa,也不会有security,所以这大段代码主要是加一些环境变量,然后带上参数去启动org.apache.catalina.startup.Bootstrap的main方法。
五、总结
启动tomcat可以双击startup.bat,也可以在命令行直接catalina.bat run,大致的流程是这样:
首先判断一下用户直接使用 catalina.bat run 来启动 Tocmat;
设置 CATALINA_HOME 和 CATALINA_BASE 环境变量值;
验证 CATALINA_HOME 和 CATALINA_BASE 环境变量值的正确性;
调用 setnv.bat 脚本;
调用 setclasspath.bat 脚本;
添加 bootstrap.jar 和 tomcat-juli.jar 到 CLASSPATH 中;
设置 CATALINA_TMPDIR 临时目录的值为 Tomcat 目录下的 temp;
追加一系列的参数到 JAVA_OPTS 中;
整合相关的启动信息,参数;
启动 Tomcat。