Tomcat下startup.bat解读

:: 执行代码
SET "JAVA_HOME=D:\Program Files\Java\jdk1.7.0_80"
SET "CATALINA_HOME=D:\Program Files\apache-tomcat-7.0.75"

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
goto end

:okHome
set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat"
if exist "%EXECUTABLE%" goto okExec
goto end

:okExec
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
:: %cd%为当前目录 
set "CURRENT_DIR=%cd%"
:: 如果设置了[CATALINA_HOME]变量就跳转到下面getHome处
if not "%CATALINA_HOME%" == "" goto gotHome
set "CATALINA_HOME=%CURRENT_DIR%"、
:: 如果存在下面catalina.bat这个文件就回退到上一级目录并设置成CATALINA_HOME
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
cd ..
set "CATALINA_HOME=%cd%"
cd "%CURRENT_DIR%"  

:: DOS语法:1.可以通过【goto】执行以下命令;2.可以通过顺序执行以下命令
: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


:: 【EXECUTABLE】可执行的,这里设置要执行的文件
: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

::执行【catalina.bat】文件
:doneSetArgs
call "%EXECUTABLE%" start %CMD_LINE_ARGS%

:end

这个批处理文件,主要是正确的找到catalina.bat,并且执行。

当把tomcat解压到一个地方时,通常是设置一个CATALINA_HOME的环境变量,执行这个文件时,就很容易找到 catalina.bat
如果没有设置CATALINA_HOME的环境变量,那么你需要在dos中进入tomcat的安装目录,或者bin...子目录,再执行这个文件,如果在其他目录,将会出现提示找不到文件。 【针对startup.bat文件】
双击该文件时,%cd%的值为当前目录的值,所有执行是没有问题的。

你可能感兴趣的:(Tomcat下startup.bat解读)