运行startup.bat的启动过程

一、前言

一般启动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窗口。

可以参考一下这篇博文:

Bat命令学习

三、startup.bat

用编辑器打开startup.bat文件,内容如下:

 @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 --------------------------------------------------------------------------- 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 ..
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 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 Get remaining unshifted command line arguments and save them in the
set CMD_LINE_ARGS=
:setArgs
if ""%""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %
shift
goto setArgs
:doneSetArgs call "%EXECUTABLE%" start %CMD_LINE_ARGS% :end

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

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

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

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

第一段:判断CATALINA_HOME是否定义

 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 ..
set "CATALINA_HOME=%cd%"
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标签

 :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

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

第三段:执行okHome标签

 :okHome

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

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

第四段:再次检查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

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

第五段:

 :okExec

 rem Get remaining unshifted command line arguments and save them in the
set CMD_LINE_ARGS=
:setArgs
if ""%""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %
shift
goto setArgs
:doneSetArgs 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标签

 :end

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

四、catalina.bat

如果startup.bat执行正确,就等同于运行catalina.bat start,进入了catalina.bat脚本中,将脚本前面的一大段注释删掉了,如下:

 setlocal

 rem Suppress Terminate batch job on CTRL+C
if not ""%"" == ""run"" goto mainEntry
if "%TEMP%" == "" goto mainEntry
if exist "%TEMP%\%~nx0.run" goto mainEntry
echo Y>"%TEMP%\%~nx0.run"
if not exist "%TEMP%\%~nx0.run" goto mainEntry
echo Y>"%TEMP%\%~nx0.Y"
call "%~f0" %* <"%TEMP%\%~nx0.Y"
rem Use provided errorlevel
set RETVAL=%ERRORLEVEL%
del /Q "%TEMP%\%~nx0.Y" >NUL >&
exit /B %RETVAL%
:mainEntry
del /Q "%TEMP%\%~nx0.run" >NUL >& 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 rem Copy CATALINA_BASE from CATALINA_HOME if not defined
if not "%CATALINA_BASE%" == "" goto gotBase
set "CATALINA_BASE=%CATALINA_HOME%"
:gotBase rem Ensure that neither CATALINA_HOME nor CATALINA_BASE contains a semi-colon
rem as this is used as the separator in the classpath and Java provides no
rem mechanism for escaping if the same character appears in the path. Check this
rem by replacing all occurrences of ';' with '' and checking that neither
rem CATALINA_HOME nor CATALINA_BASE have changed
if "%CATALINA_HOME%" == "%CATALINA_HOME:;=%" goto homeNoSemicolon
echo Using CATALINA_HOME: "%CATALINA_HOME%"
echo Unable to start as CATALINA_HOME contains a semicolon (;) character
goto end
:homeNoSemicolon if "%CATALINA_BASE%" == "%CATALINA_BASE:;=%" goto baseNoSemicolon
echo Using CATALINA_BASE: "%CATALINA_BASE%"
echo Unable to start as CATALINA_BASE contains a semicolon (;) character
goto end
:baseNoSemicolon rem Ensure that any user defined CLASSPATH variables are not used on startup,
rem but allow them to be specified in setenv.bat, in rare case when it is needed.
set CLASSPATH= rem Get standard environment variables
if not exist "%CATALINA_BASE%\bin\setenv.bat" goto checkSetenvHome
call "%CATALINA_BASE%\bin\setenv.bat"
goto setenvDone
:checkSetenvHome
if exist "%CATALINA_HOME%\bin\setenv.bat" call "%CATALINA_HOME%\bin\setenv.bat"
:setenvDone rem Get standard Java environment variables
if exist "%CATALINA_HOME%\bin\setclasspath.bat" goto okSetclasspath
echo Cannot find "%CATALINA_HOME%\bin\setclasspath.bat"
echo This file is needed to run this program
goto end
:okSetclasspath
call "%CATALINA_HOME%\bin\setclasspath.bat" %
if errorlevel goto end rem Add on extra jar file to CLASSPATH
rem Note that there are no quotes as we do not want to introduce random
rem quotes into the CLASSPATH
if "%CLASSPATH%" == "" goto emptyClasspath
set "CLASSPATH=%CLASSPATH%;"
:emptyClasspath
set "CLASSPATH=%CLASSPATH%%CATALINA_HOME%\bin\bootstrap.jar" if not "%CATALINA_TMPDIR%" == "" goto gotTmpdir
set "CATALINA_TMPDIR=%CATALINA_BASE%\temp"
:gotTmpdir rem Add tomcat-juli.jar to classpath
rem tomcat-juli.jar can be over-ridden per instance
if not exist "%CATALINA_BASE%\bin\tomcat-juli.jar" goto juliClasspathHome
set "CLASSPATH=%CLASSPATH%;%CATALINA_BASE%\bin\tomcat-juli.jar"
goto juliClasspathDone
:juliClasspathHome
set "CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\bin\tomcat-juli.jar"
:juliClasspathDone if not "%JSSE_OPTS%" == "" goto gotJsseOpts
set JSSE_OPTS="-Djdk.tls.ephemeralDHKeySize=2048"
:gotJsseOpts
set "JAVA_OPTS=%JAVA_OPTS% %JSSE_OPTS%" rem Register custom URL handlers
rem Do this here so custom URL handles (specifically 'war:...') can be used in the security policy
set "JAVA_OPTS=%JAVA_OPTS% -Djava.protocol.handler.pkgs=org.apache.catalina.webresources" if not "%LOGGING_CONFIG%" == "" goto noJuliConfig
set LOGGING_CONFIG=-Dnop
if not exist "%CATALINA_BASE%\conf\logging.properties" goto noJuliConfig
set LOGGING_CONFIG=-Djava.util.logging.config.file="%CATALINA_BASE%\conf\logging.properties"
:noJuliConfig if not "%LOGGING_MANAGER%" == "" goto noJuliManager
set LOGGING_MANAGER=-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
:noJuliManager rem Configure JAVA specific start-up parameters
set "JDK_JAVA_OPTIONS=%JDK_JAVA_OPTIONS% --add-opens=java.base/java.lang=ALL-UNNAMED"
set "JDK_JAVA_OPTIONS=%JDK_JAVA_OPTIONS% --add-opens=java.base/java.io=ALL-UNNAMED"
set "JDK_JAVA_OPTIONS=%JDK_JAVA_OPTIONS% --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED" rem Java no longer supports the java.endorsed.dirs
rem system property. Only try to use it if
rem JAVA_ENDORSED_DIRS was explicitly set
rem or CATALINA_HOME/endorsed exists.
set ENDORSED_PROP=ignore.endorsed.dirs
if "%JAVA_ENDORSED_DIRS%" == "" goto noEndorsedVar
set ENDORSED_PROP=java.endorsed.dirs
goto doneEndorsed
:noEndorsedVar
if not exist "%CATALINA_HOME%\endorsed" goto doneEndorsed
set ENDORSED_PROP=java.endorsed.dirs
:doneEndorsed rem ----- Execute The Requested Command --------------------------------------- echo Using CATALINA_BASE: "%CATALINA_BASE%"
echo Using CATALINA_HOME: "%CATALINA_HOME%"
echo Using CATALINA_TMPDIR: "%CATALINA_TMPDIR%"
if ""%"" == ""debug"" goto use_jdk
echo Using JRE_HOME: "%JRE_HOME%"
goto java_dir_displayed
:use_jdk
echo Using JAVA_HOME: "%JAVA_HOME%"
:java_dir_displayed
echo Using CLASSPATH: "%CLASSPATH%" set _EXECJAVA=%_RUNJAVA%
set MAINCLASS=org.apache.catalina.startup.Bootstrap
set ACTION=start
set SECURITY_POLICY_FILE=
set DEBUG_OPTS=
set JPDA= if not ""%"" == ""jpda"" goto noJpda
set JPDA=jpda
if not "%JPDA_TRANSPORT%" == "" goto gotJpdaTransport
set JPDA_TRANSPORT=dt_socket
:gotJpdaTransport
if not "%JPDA_ADDRESS%" == "" goto gotJpdaAddress
set JPDA_ADDRESS=localhost:
:gotJpdaAddress
if not "%JPDA_SUSPEND%" == "" goto gotJpdaSuspend
set JPDA_SUSPEND=n
:gotJpdaSuspend
if not "%JPDA_OPTS%" == "" goto gotJpdaOpts
set JPDA_OPTS=-agentlib:jdwp=transport=%JPDA_TRANSPORT%,address=%JPDA_ADDRESS%,server=y,suspend=%JPDA_SUSPEND%
:gotJpdaOpts
shift
:noJpda if ""%"" == ""debug"" goto doDebug
if ""%"" == ""run"" goto doRun
if ""%"" == ""start"" goto doStart
if ""%"" == ""stop"" goto doStop
if ""%"" == ""configtest"" goto doConfigTest
if ""%"" == ""version"" goto doVersion echo Usage: catalina ( commands ... )
echo commands:
echo debug Start Catalina in a debugger
echo debug -security Debug Catalina with a security manager
echo jpda start Start Catalina under JPDA debugger
echo run Start Catalina in the current window
echo run -security Start in the current window with security manager
echo start Start Catalina in a separate window
echo start -security Start in a separate window with security manager
echo stop Stop Catalina
echo configtest Run a basic syntax check on server.xml
echo version What version of tomcat are you running?
goto end :doDebug
shift
set _EXECJAVA=%_RUNJDB%
set DEBUG_OPTS=-sourcepath "%CATALINA_HOME%\..\..\java"
if not ""%"" == ""-security"" goto execCmd
shift
echo Using Security Manager
set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
goto execCmd :doRun
shift
if not ""%"" == ""-security"" goto execCmd
shift
echo Using Security Manager
set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
goto execCmd :doStart
shift
if "%TITLE%" == "" set TITLE=Tomcat
set _EXECJAVA=start "%TITLE%" %_RUNJAVA%
if not ""%"" == ""-security"" goto execCmd
shift
echo Using Security Manager
set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
goto execCmd :doStop
shift
set ACTION=stop
set CATALINA_OPTS=
goto execCmd :doConfigTest
shift
set ACTION=configtest
set CATALINA_OPTS=
goto execCmd :doVersion
%_EXECJAVA% -classpath "%CATALINA_HOME%\lib\catalina.jar" org.apache.catalina.util.ServerInfo
goto end :execCmd
rem Get remaining unshifted command line arguments and save them in the
set CMD_LINE_ARGS=
:setArgs
if ""%""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %
shift
goto setArgs
:doneSetArgs rem Execute Java with the applicable properties
if not "%JPDA%" == "" goto doJpda
if not "%SECURITY_POLICY_FILE%" == "" goto doSecurity
%_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%
goto end
:doSecurity
%_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%
goto end
:doJpda
if not "%SECURITY_POLICY_FILE%" == "" goto doSecurityJpda
%_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%
goto end
:doSecurityJpda
%_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%
goto end :end

第一段:

 rem Suppress Terminate batch job on CTRL+C
if not ""%"" == ""run"" goto mainEntry
if "%TEMP%" == "" goto mainEntry
if exist "%TEMP%\%~nx0.run" goto mainEntry
echo Y>"%TEMP%\%~nx0.run"
if not exist "%TEMP%\%~nx0.run" goto mainEntry
echo Y>"%TEMP%\%~nx0.Y"
call "%~f0" %* <"%TEMP%\%~nx0.Y"
rem Use provided errorlevel
set RETVAL=%ERRORLEVEL%
del /Q "%TEMP%\%~nx0.Y" >NUL >&
exit /B %RETVAL%
:mainEntry
del /Q "%TEMP%\%~nx0.run" >NUL >&
  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 文件。

这段代码的作用就是调用本身,判断临时目录中的文件是否存在来避免二次回调自己

第二段:

 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 rem Copy CATALINA_BASE from CATALINA_HOME if not defined
if not "%CATALINA_BASE%" == "" goto gotBase
set "CATALINA_BASE=%CATALINA_HOME%"
: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 的值。

第三段:

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

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

第四段:

 rem Ensure that any user defined CLASSPATH variables are not used on startup,
rem but allow them to be specified in setenv.bat, in rare case when it is needed.
set CLASSPATH= rem Get standard environment variables
if not exist "%CATALINA_BASE%\bin\setenv.bat" goto checkSetenvHome
call "%CATALINA_BASE%\bin\setenv.bat"
goto setenvDone
:checkSetenvHome
if exist "%CATALINA_HOME%\bin\setenv.bat" call "%CATALINA_HOME%\bin\setenv.bat"
:setenvDone rem Get standard Java environment variables
if exist "%CATALINA_HOME%\bin\setclasspath.bat" goto okSetclasspath
echo Cannot find "%CATALINA_HOME%\bin\setclasspath.bat"
echo This file is needed to run this program
goto end
:okSetclasspath
call "%CATALINA_HOME%\bin\setclasspath.bat" %
if errorlevel 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。

第五段:

 rem Add on extra jar file to CLASSPATH
rem Note that there are no quotes as we do not want to introduce random
rem quotes into the CLASSPATH
if "%CLASSPATH%" == "" goto emptyClasspath
set "CLASSPATH=%CLASSPATH%;"
:emptyClasspath
set "CLASSPATH=%CLASSPATH%%CATALINA_HOME%\bin\bootstrap.jar" if not "%CATALINA_TMPDIR%" == "" goto gotTmpdir
set "CATALINA_TMPDIR=%CATALINA_BASE%\temp"
:gotTmpdir rem Add tomcat-juli.jar to classpath
rem tomcat-juli.jar can be over-ridden per instance
if not exist "%CATALINA_BASE%\bin\tomcat-juli.jar" goto juliClasspathHome
set "CLASSPATH=%CLASSPATH%;%CATALINA_BASE%\bin\tomcat-juli.jar"
goto juliClasspathDone
:juliClasspathHome
set "CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\bin\tomcat-juli.jar"
:juliClasspathDone

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

  • 把 Tomcat bin 目录下的 bootstrap.jar 加入到环境变量中;
  • 设置 CATALINA_TMPDIR 环境变量的值为 Tomcat 目录下的 temp 目录;
  • 把 Tomcat bin 目录下的 tomcat-juli.jar 加入到环境变量中;

第六段:

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

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

 rem Java  no longer supports the java.endorsed.dirs
rem system property. Only try to use it if
rem JAVA_ENDORSED_DIRS was explicitly set
rem or CATALINA_HOME/endorsed exists.
set ENDORSED_PROP=ignore.endorsed.dirs
if "%JAVA_ENDORSED_DIRS%" == "" goto noEndorsedVar
set ENDORSED_PROP=java.endorsed.dirs
goto doneEndorsed
:noEndorsedVar
if not exist "%CATALINA_HOME%\endorsed" goto doneEndorsed
set ENDORSED_PROP=java.endorsed.dirs
:doneEndorsed

第七段:

 echo Using CATALINA_BASE:   "%CATALINA_BASE%"
echo Using CATALINA_HOME: "%CATALINA_HOME%"
echo Using CATALINA_TMPDIR: "%CATALINA_TMPDIR%"
if ""%"" == ""debug"" goto use_jdk
echo Using JRE_HOME: "%JRE_HOME%"
goto java_dir_displayed
:use_jdk
echo Using JAVA_HOME: "%JAVA_HOME%"
:java_dir_displayed
echo Using CLASSPATH: "%CLASSPATH%"

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

第八段:

 set _EXECJAVA=%_RUNJAVA%
set MAINCLASS=org.apache.catalina.startup.Bootstrap
set ACTION=start
set SECURITY_POLICY_FILE=
set DEBUG_OPTS=
set JPDA=

设置一些环境变量:

  1. _RUNJAVA : %JRE_HOME%\bin\java.exe;

  2. MAINCLASS : 指定了 Tomcat 的启动类;

  3. ACTION : 动作, 就是启动;

  4. SECURITY_POLICY_FILE : 安全策略文件,如果启动的时候加上了 -security 参数的话,下面会对这个参数指定到 Tomcat 的 conf 目录下的 catalina.policy 文件;

  5. JPDA : 还不清楚 。

第九段:

 if not ""%"" == ""jpda"" goto noJpda
set JPDA=jpda
if not "%JPDA_TRANSPORT%" == "" goto gotJpdaTransport
set JPDA_TRANSPORT=dt_socket
:gotJpdaTransport
if not "%JPDA_ADDRESS%" == "" goto gotJpdaAddress
set JPDA_ADDRESS=localhost:
:gotJpdaAddress
if not "%JPDA_SUSPEND%" == "" goto gotJpdaSuspend
set JPDA_SUSPEND=n
:gotJpdaSuspend
if not "%JPDA_OPTS%" == "" goto gotJpdaOpts
set JPDA_OPTS=-agentlib:jdwp=transport=%JPDA_TRANSPORT%,address=%JPDA_ADDRESS%,server=y,suspend=%JPDA_SUSPEND%
:gotJpdaOpts
shift
:noJpda

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

第十段:

 if ""%"" == ""debug"" goto doDebug
if ""%"" == ""run"" goto doRun
if ""%"" == ""start"" goto doStart
if ""%"" == ""stop"" goto doStop
if ""%"" == ""configtest"" goto doConfigTest
if ""%"" == ""version"" goto doVersion echo Usage: catalina ( commands ... )
echo commands:
echo debug Start Catalina in a debugger
echo debug -security Debug Catalina with a security manager
echo jpda start Start Catalina under JPDA debugger
echo run Start Catalina in the current window
echo run -security Start in the current window with security manager
echo start Start Catalina in a separate window
echo start -security Start in a separate window with security manager
echo stop Stop Catalina
echo configtest Run a basic syntax check on server.xml
echo version What version of tomcat are you running?
goto end

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

第十一段:

 :doDebug
shift
set _EXECJAVA=%_RUNJDB%
set DEBUG_OPTS=-sourcepath "%CATALINA_HOME%\..\..\java"
if not ""%"" == ""-security"" goto execCmd
shift
echo Using Security Manager
set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
goto execCmd :doRun
shift
if not ""%"" == ""-security"" goto execCmd
shift
echo Using Security Manager
set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
goto execCmd :doStart
shift
if "%TITLE%" == "" set TITLE=Tomcat
set _EXECJAVA=start "%TITLE%" %_RUNJAVA%
if not ""%"" == ""-security"" goto execCmd
shift
echo Using Security Manager
set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
goto execCmd :doStop
shift
set ACTION=stop
set CATALINA_OPTS=
goto execCmd :doConfigTest
shift
set ACTION=configtest
set CATALINA_OPTS=
goto execCmd :doVersion
%_EXECJAVA% -classpath "%CATALINA_HOME%\lib\catalina.jar" org.apache.catalina.util.ServerInfo
goto end

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

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

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

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

最后都跳到了 execCmd 标签处。

第十二段:

 :execCmd
rem Get remaining unshifted command line arguments and save them in the
set CMD_LINE_ARGS=
:setArgs
if ""%""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %
shift
goto setArgs
:doneSetArgs

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

最后一段:

 rem Execute Java with the applicable properties
if not "%JPDA%" == "" goto doJpda
if not "%SECURITY_POLICY_FILE%" == "" goto doSecurity
%_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%
goto end
:doSecurity
%_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%
goto end
:doJpda
if not "%SECURITY_POLICY_FILE%" == "" goto doSecurityJpda
%_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%
goto end
:doSecurityJpda
%_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%
goto end :end

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

五、总结

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

  1. 首先判断一下用户直接使用 catalina.bat run 来启动 Tocmat;

  2. 设置 CATALINA_HOME 和 CATALINA_BASE 环境变量值;

  3. 验证 CATALINA_HOME 和 CATALINA_BASE 环境变量值的正确性;

  4. 调用 setnv.bat 脚本;

  5. 调用 setclasspath.bat 脚本;

  6. 添加 bootstrap.jar 和 tomcat-juli.jar 到 CLASSPATH 中;

  7. 设置 CATALINA_TMPDIR 临时目录的值为 Tomcat 目录下的 temp;

  8. 追加一系列的参数到 JAVA_OPTS 中;

  9. 整合相关的启动信息,参数;

  10. 启动 Tomcat。

上一篇:Poj 2503 / OpenJudge 2503 Babelfish


下一篇:Poj 2159 / OpenJudge 2159 Ancient Cipher