@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 ---------------------------------------------------------------------------
rem 这个脚本还是挺简单的, 目的就是找到 catalina.bat 并调用它.
rem 开始与终止批处理文件中环境改动的本地化操作。在执行 Setlocal 之后所做的环境改动只限于批处理文件。要还原原先的设置,必须执行 Endlocal。达到批处理文件结尾时,对于该批处理文件的每个尚未执行的 Setlocal 命令,都会有一个隐含的 Endlocal 被执行。Endlocal结束批处理文件中环境改动的本地化操作。在执行Endlocal 之后所做的环境改动不再仅限于批处理文件。批处理文件结束后,原先的设置无法还原。
setlocal
rem Guess CATALINA_HOME if not defined
rem 将当期目录设置给CURRENT_DIR目录
set "CURRENT_DIR=%cd%"
rem 如果设置了系统环境变量CATALINA_HOME(用户环境变量只有Administrator可以取到)(环境变量应该是Tomcat根目录),直接到gotHome
if not "%CATALINA_HOME%" == "" goto gotHome
rem 如果未设置CATALINA_HOME系统环境变量,将当期路径设置为CATALINA_HOME(此处路径是apache-tomcat-9.0.6\bin,下面查找apache-tomcat-9.0.6\bin\bin\catalina.bat不存在)
set "CATALINA_HOME=%CURRENT_DIR%"
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
rem 将当期目录上级目录apache-tomcat-9.0.6再设为CATALINA_HOME并切换回之前的apache-tomcat-9.0.6\bin
cd ..
set "CATALINA_HOME=%cd%"
cd "%CURRENT_DIR%"
:gotHome
rem 如果apache-tomcat-9.0.6\bin\bin\catalina.bat文件存在就到okHome,否则提示并结束
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 EXECUTABLE变量为catalina.bat文件的路径
set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat"
rem Check that target executable exists
rem 如果EXECUTABLE存在就调到okExec,否则退出
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
rem 如果有参数就循环将参数用空格拼接起来,没有参数就执行doneSetArgs
set CMD_LINE_ARGS=
:setArgs
if ""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
shift
goto setArgs
:doneSetArgs
rem call命令调用另一个批处理程序,并且不终止父批处理程序(如果不用call而直接调用别的批处理文件,那么执行完那个批处理文件后将无法返回当前文件并执行当前文件的后续命令)
rem call 从批处理程序调用另一个批处理程序。
rem start 启动一个单独的窗口以运行指定的程序或命令。
rem 这个利用 call 命令执行调用, 并把参数传进去
rem 这里实际上是调用了 catalina.bat 这个脚本, 然后传递一个 start 参数给它.如果我们在 cmd 中运行 startup.bat 并且后面跟着一些参数的话, 这里也一起传递过去了.这里实际上就是执行了: %CATALINA_HOME%\bin\catalina.bat start
call "%EXECUTABLE%" start %CMD_LINE_ARGS%
:end