tomcat的启动一般是从startup.bat/startup.sh开始,然后启动catalina.bat/catalina.bat,然后启动catalina.jar包
那么它们启动的时候都做了哪些事情呢?
下边从startup.bat开始(// TODO startup.sh暂缓)理一下启动过程中都发生了什么。
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 script for the CATALINA Server 20 rem --------------------------------------------------------------------------- 21 22 rem Guess CATALINA_HOME if not defined 23 set "CURRENT_DIR=%cd%" 24 if not "%CATALINA_HOME%" == "" goto gotHome 25 set "CATALINA_HOME=%CURRENT_DIR%" 26 if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome 27 cd .. 28 set "CATALINA_HOME=%cd%" 29 cd "%CURRENT_DIR%" 30 :gotHome 31 if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome 32 echo The CATALINA_HOME environment variable is not defined correctly 33 echo This environment variable is needed to run this program 34 goto end 35 :okHome 36 37 set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat" 38 39 rem Check that target executable exists 40 if exist "%EXECUTABLE%" goto okExec 41 echo Cannot find "%EXECUTABLE%" 42 echo This file is needed to run this program 43 goto end 44 :okExec 45 46 rem Get remaining unshifted command line arguments and save them in the 47 set CMD_LINE_ARGS= 48 :setArgs 49 if ""%1""=="""" goto doneSetArgs 50 set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1 51 shift 52 goto setArgs 53 :doneSetArgs 54 55 56 call "%EXECUTABLE%" start %CMD_LINE_ARGS% 57 58 :end
line1 @echo off @关闭本命令回显,echo off命令是关闭输出所执行的批处理操作
line17 if "%OS%" == "Windows_NT" setlocal 如果有OS这个环境变量且值为Windows_NT,开始设置本地环境变量。其中setlocal命令的解释是:Starts localization of environment variables in a batch file. Localization continues until a matching endlocal command is encountered or the end of the batch file is reached. 即在批处理文件中开始设置本地化环境变量,这些变量在遇到与之(setlocal)对应的endlocal命令或者bat文件结束前一直有效。刚才发现电脑上另一个版本的tomcat(7.0.67)中已经取消了OS变量的判断而直接setlocal。
line23-35 Guess CATALINA_HOME if not defined,找CATALINA_HOME这个变量,判断catalina.bat是否存在。
rem Guess CATALINA_HOME if not defined set "CURRENT_DIR=%cd%" if not "%CATALINA_HOME%" == "" goto gotHome set "CATALINA_HOME=%CURRENT_DIR%" if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome cd .. set "CATALINA_HOME=%cd%" cd "%CURRENT_DIR%" :gotHome if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome echo The CATALINA_HOME environment variable is not defined correctly echo This environment variable is needed to run this program goto end :okHome
line23 set "CURRENT_DIR=%cd%" CURRENT_DIR就是启动后第一个临时环境变量,这个环境变量的值是%cd%,"%cd%" 获取的是bat文件执行的当前目录,cd = current_dir的缩写,即C:\tool\apache-tomcat-7.0.67\bin。
line24 if not "%CATALINA_HOME%" == "" goto gotHome 如果设置了CATALINA_HOME环境变量,goto gotHome节点,但我估计开发很少有设置这个环境变量的(反正我从不);没有这个变量的话继续。
line25 set "CATALINA_HOME=%CURRENT_DIR%" 设置CATALINA_HOME(算是又一个临时环境变量)为当前bat文件执行目录, 即C:\tool\apache-tomcat-7.0.67\bin。
line 26 if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome 如果%CATALINA_HOME%E\bin\下的catalina.bat存在的话,goto okHome节点。默认安装的tomcat里catalina.bat和startup.bat都在%CATALINA_HOME%E\bin\下。
line27 cd .. 如果在%CATALINA_HOME%E\bin\没有catalina.bat文件,回到父目录即C:\tool\apache-tomcat-7.0.67。
line28 set "CATALINA_HOME=%cd%" 然后改变CATALINA_HOME=C:\tool\apache-tomcat-7.0.67。
line29 cd "%CURRENT_DIR%" 回到C:\tool\apache-tomcat-7.0.67\bin。
line31 if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome 再次判断catalina.bat是否存在,如果存在goto okHome节点;不存在执行line32-33的输出,然后goto end节点,本次批处理结束。
line37 set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat" 设置executable节点
line39-44 再次判断%CATALINA_HOME%\bin\catalina.bat是否存在,如果存在goto okExec节点;不存在goto end节点,本次批处理结束。
line46-53 设置CMD_LINE_ARGS
set CMD_LINE_ARGS= :setArgs if ""%1""=="""" goto doneSetArgs set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1 shift goto setArgs :doneSetArgs
line47-48 将: setArgs节点下的返回结果赋值给CMD_LINE_ARGS变量。
line49-52 如果%1也就是第一个参数为空,goto doneSetArgs;否则循环所有的参数,将所有的参数加上一个空格后拼接在一起,赋值给CMD_LINE_ARGS变量,全部参数处理完后执行line56
shift命令 批处理文件中可引用的参数为%0~%9,%0是指批处理文件的本身,也可以说是一个外部命令: %1~%9是批处理参数,也称形参: 而替换形参的实参若超过了批处理文件中所规定数值(9个)且想在批处理文件中应用这些实参的话,shift命令可以帮你实现!它更改批处理文件中可替换参数的位置,shift [/n] n的取值是[0,8],且为整数;[/n]为可选参数,当赋予n某个值时,就意味着命令从第n个参数开始移位;当n赋予的值为0,1或不带有任何命令选项的shift时,则表示批处理文件中替换参数左移一个位置,后面的替换参数陆续填补上去,直至可替换参数为空。
shift命令
line56 call "%EXECUTABLE%" start %CMD_LINE_ARGS% 调用EXECUTABLE参数为start和上边拼接CMD_LINE_ARGS(可能为空)。
至此,startup.bat结束,可以看出它主要的目的是找到并启动catalina.bat。