tomcat startup.bat解读

startup.bat注释版:
rem 该文件的主要作用是找到catalina.bat并调用调用catalina.bat start
@echo off                              rem 关闭回显,不显示下面的命令;“echo off”也是命令,它本身也会显示,如果连这条也不显示,就在前面加个“@”。
echo Enter the startup.bat file ......
if "%OS%" == "Windows_NT" setlocal     rem 判断当前系统是否为Windows系统
rem rem表示注释
rem ---------------------------------------------------------------------------
rem Start script for the CATALINA Server
rem
rem $Id: startup.bat 895392 2010-01-03 14:02:31Z kkolinko $
rem ---------------------------------------------------------------------------

rem Guess CATALINA_HOME if not defined        rem 猜测CATALINA_HOME是否没有定义
set "CURRENT_DIR=%cd%"                        rem 设置CURRENT_DIR环境变量为当前目录E:\code\web\tomcat\bin  注:%cd%的值为当前目录的值
echo CURRENT_DIR:"%CURRENT_DIR%"              rem 打印结果 CURRENT_DIR:"E:\code\web\tomcat\bin"
if not "%CATALINA_HOME%" == "" goto gotHome   rem 如果设置了CATALINA_HOME环境变量 ,就直接到下面的gotHome处
set "CATALINA_HOME=%CURRENT_DIR%"             rem 如果没有设置CATALINA_HOME环境变量,就设置CATALINA_HOME为CURRENT_DIR(当前目录)
echo before CATALINA_HOME:"%CATALINA_HOME%"

echo "%CATALINA_HOME%\bin\catalina.bat" is wrong           rem E:\code\web\tomcat\bin\bin\catalina.bat
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome    rem 如果在指定目录是否能找到catalina.bat,找到了就直接跳到下面的okHome处
cd ..                                                      rem 如果没有找到,就退到当前目录的上一级目录
echo cd .. :"%cd%"                                         rem 打印结果 cd .. :"E:\code\web\tomcat"
set "CATALINA_HOME=%cd%"                                   rem 重新设置CATALINA_HOME环境变量为E:\code\web\tomcat
echo now CATALINA_HOME:"%CATALINA_HOME%"                   rem 打印结果 CATALINA_HOME:"E:\code\web\tomcat"
cd "%CURRENT_DIR%"                                         rem 进入CURRENT_DIR环境变量代表的目录E:\code\web\tomcat\bin

:gotHome
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome    rem 再次判断catalina.bat是否找到了,找到了就直接跳到下面的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 设置环境变量EXECUTABLE,即表示要执行的文件

rem Check that target executable exists
if exist "%EXECUTABLE%" goto okExec                        rem 判断catalina.bat是否找到了,找到了就直接跳到下面的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=                                         rem 这里是设置命令行参数

:setArgs
rem %[1-9]表示参数,参数是指在运行批处理文件时在文件名后加的以空格(或者Tab)分隔的字符串。变量可以从%0到%9,%0表示批处理命令本身,其它参数字符串用%1到%9顺序表示。
if ""%1""=="""" goto doneSetArgs                           rem 判断参数是否有参数,没有就直接跳到doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1                       rem 如果有参数,将参数放入CMD_LINE_ARGS里,加在CMD_LINE_ARGS变量的后面
shift                                                      rem 把变量%2里移到%1
goto setArgs                                               rem 返回setArgs位置,进行循环

:doneSetArgs
echo "%EXECUTABLE%" start %CMD_LINE_ARGS%                  rem 查看具体调用形式
call "%EXECUTABLE%" start %CMD_LINE_ARGS%                  rem 调用"%EXECUTABLE%" start并加上参数(即执行catalina.bat)

:end

rem startup.bat脚本实际上并没有做什么实际的工作,主要功能是在tomcat/bin/或tomcat/目录下正确的找到catalina.bat,并调用catalina.bat start命令。
rem 即startup.bat的主要作用是调用catalina.bat start命令(即其与命令行中执行catalina.bat start作用一样)。


startup.bat使用版:
@echo off
echo Enter the startup.bat file ......
if "%OS%" == "Windows_NT" setlocal
rem ---------------------------------------------------------------------------
rem Start script for the CATALINA Server
rem
rem $Id: startup.bat 895392 2010-01-03 14:02:31Z kkolinko $
rem ---------------------------------------------------------------------------

rem Guess CATALINA_HOME if not defined
set "CURRENT_DIR=%cd%"
rem echo CURRENT_DIR:"%CURRENT_DIR%"
if not "%CATALINA_HOME%" == "" goto gotHome
set "CATALINA_HOME=%CURRENT_DIR%"
rem echo before CATALINA_HOME:"%CATALINA_HOME%"

rem echo "%CATALINA_HOME%\bin\catalina.bat" is wrong
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
cd ..
rem echo cd .. :"%cd%"
set "CATALINA_HOME=%cd%"
rem echo now CATALINA_HOME:"%CATALINA_HOME%"
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 ""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
shift
goto setArgs

:doneSetArgs
echo "%EXECUTABLE%" start %CMD_LINE_ARGS%
call "%EXECUTABLE%" start %CMD_LINE_ARGS%

:end

你可能感兴趣的:(startup)