启动tomcat,批处理文件分析

startup.bat(对startup.bat 进行了适当的裁剪):

1 @echo off

//echo off 命令用于让其后的所有命令在执行时都不在命令行窗口中显示命令自身的文本内容。如果在某个命令前加@,如@echo off,这个命令执行时将不在命令行窗口中显示命令自身的文本内容,即echo off命令不显示。

2 if "%OS%" == "Windows_NT" setlocal

//OS环境变量的值是否为Windows_NT。可以在命令行窗口中执行set OS查看OS环境变量的值,执行help setlocal命令查看setlocal的意义。在执行setlocal之后所做的环境改动只限于批处理文件。

3 rem Guess CATALINA_HOME if not defined

//rem命令用于注释一行文本

4 if not "%CATALINA_HOME%" == "" goto gotHome

//如果CATALINA_HOME环境变量不等于空字符时,批处理跳转到gotHome所在行,并继续往下执行
5 set "CATALINA_HOME=%CURRENT_DIR%"

//设置CATALINA_HOME环境变量
6 if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
7 cd ..

//如果CATALINA_HOME环境变量下不存在catalina.bat文件,进入上一级目录
8 set "CATALINA_HOME=%cd%"

//设置CATALINA_HOME环境变量为当前目录
9 cd "%CURRENT_DIR%"
10 :gotHome
11 if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
12 echo ...................
13 goto end
14 :okHome

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

//设置EXECUTABLE环境变量
16 if exist "%EXECUTABLE%" goto okExec
17 echo ..............................
18 goto end
19 :okExec
20 set CMD_LINE_ARGS=

//将CMD_LINE_ARGS环境变量的值清空
21 :setArgs
22 if ""%1""=="""" goto doneSetArgs

//批处理文件中可以使用%0到%9。%0的值为正在执行的批处理文件名,%1到%9依次为传递给这个批处理文件的前9个参数。例如在命令行窗口中执行catalina.bat start,start就是一个传递给catalina.bat的参数
23 set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1

//将参数拼接到CMD_LINE_ARGS环境变量中
24 shift

//shift用来移动参数。%2的值移到%1的参数中,%3的值移到%2的参数中,%4的值移到%3的参数中,依次类推。
25 goto setArgs

//批处理跳转到setArgs行,继续处理下一个参数,相当于循环拼接所有参数。
26 :doneSetArgs

 

//开启远程调试Tomcat,jpda调试默认端口是8000

//set JPDA_TRANSPORT=dt_socket
//set JPDA_ADDRESS=8000
//set JPDA_SUSPEND=y

//call "%EXECUTABLE%" jpdastart %CMD_LINE_ARGS%

27 call "%EXECUTABLE%" start %CMD_LINE_ARGS%

//执行带参数的catalina.bat脚本

28 :end

 

 

------------------------------

catalina.bat

set "CURRENT_DIR=%cd%"
if not "%CATALINA_HOME%" == "" goto gotHome
.......................
:gotHome
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
echo .........
goto end
:okHome


if not "%CATALINA_BASE%" == "" goto gotBase
set "CATALINA_BASE=%CATALINA_HOME%"

//设置CATALINA_BASE环境变量的值为CATALINA_HOME
:gotBase

set CLASSPATH=

//清空CLASSPATH环境变量的值
if not exist "%CATALINA_BASE%\bin\setenv.bat" goto checkSetenvHome
call "%CATALINA_BASE%\bin\setenv.bat"

//执行setenv.bat脚本
goto setenvDone
:checkSetenvHome
if exist "%CATALINA_HOME%\bin\setenv.bat" call "%CATALINA_HOME%\bin\setenv.bat"
:setenvDone
if exist "%CATALINA_HOME%\bin\setclasspath.bat" goto okSetclasspath
echo ...............
goto end
:okSetclasspath
set "BASEDIR=%CATALINA_HOME%"
call "%CATALINA_HOME%\bin\setclasspath.bat" %1
if errorlevel 1 goto end

//如果返回码等于指定的数字,则条件成立。setclasspath.bat会返回0或1,返回1表示异常退出,0表示正常执行完。

if not "%CATALINA_TMPDIR%" == "" goto gotTmpdir
set "CATALINA_TMPDIR=%CATALINA_BASE%\temp"
:gotTmpdir


if "%CLASSPATH%" == "" goto emptyClasspath
set "CLASSPATH=%CLASSPATH%;"
:emptyClasspath
if "%CATALINA_BASE%" == "%CATALINA_HOME%" goto juliClasspathHome
if not exist "%CATALINA_BASE%\bin\tomcat-juli.jar" goto juliClasspathHome
set "CLASSPATH=%CLASSPATH%%CATALINA_BASE%\bin\tomcat-juli.jar;%CATALINA_HOME%\bin\bootstrap.jar"
goto juliClasspathDone
:juliClasspathHome
set "CLASSPATH=%CLASSPATH%%CATALINA_HOME%\bin\bootstrap.jar"

//将bootstrap.jar添加到CLASSPATH环境变量中。因为tomcat需要将jsp页面生成的Servlet源文件编译成java字节码后运行,而那些编译java源文件的类位于bootstrap.jar中。
:juliClasspathDone

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
set JAVA_OPTS=%JAVA_OPTS% %LOGGING_CONFIG%

//添加日志配置文件到JAVA_OPTS环境变量中

.......................

echo Using CATALINA_BASE:   "%CATALINA_BASE%"
echo Using CATALINA_HOME:   "%CATALINA_HOME%"
echo Using CATALINA_TMPDIR: "%CATALINA_TMPDIR%"
if ""%1"" == ""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%

//_RUNJAVA环境变量在setclasspath.bat中定义,值等于"%JRE_HOME%\bin\java"
set MAINCLASS=org.apache.catalina.startup.Bootstrap
set ACTION=start
set SECURITY_POLICY_FILE=
set DEBUG_OPTS=
set JPDA=

if not ""%1"" == ""jpda"" goto noJpda

//jpda远程调试Tomcat,默认Jpda是不启用的
set JPDA=jpda
if not "%JPDA_TRANSPORT%" == "" goto gotJpdaTransport
set JPDA_TRANSPORT=dt_socket
:gotJpdaTransport
if not "%JPDA_ADDRESS%" == "" goto gotJpdaAddress
set JPDA_ADDRESS=8000
:gotJpdaAddress
if not "%JPDA_SUSPEND%" == "" goto gotJpdaSuspend
set JPDA_SUSPEND=n

//suspend=y这个选项很重要。如果你想从Tomcat启动的一开始就进行调试,那么就必须设置suspend=y
: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 ""%1"" == ""debug"" goto doDebug
if ""%1"" == ""run"" goto doRun
if ""%1"" == ""start"" goto doStart
if ""%1"" == ""stop"" goto doStop
if ""%1"" == ""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   version           What version of tomcat are you running?
goto end

:doDebug
shift
set _EXECJAVA=%_RUNJDB%
set DEBUG_OPTS=-sourcepath "%CATALINA_HOME%\..\..\java"
if not ""%1"" == ""-security"" goto execCmd
shift
echo Using Security Manager
set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
goto execCmd

:doRun
shift
if not ""%1"" == ""-security"" goto execCmd
shift
echo Using Security Manager
set "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"
goto execCmd

:doStart
shift
if not "%OS%" == "Windows_NT" goto noTitle
if "%TITLE%" == "" set TITLE=Tomcat
set _EXECJAVA=start "%TITLE%" %_RUNJAVA%

//start Tomcat %JRE_HOME%\bin\java
goto gotTitle
:noTitle
set _EXECJAVA=start %_RUNJAVA%
:gotTitle
if not ""%1"" == ""-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

: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 ""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
shift
goto setArgs
:doneSetArgs

rem Execute Java with the applicable properties
if not "%JPDA%" == "" goto doJpda
if not "%SECURITY_POLICY_FILE%" == "" goto doSecurity
%_EXECJAVA% %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%

//启动tomcat

//_EXECJAVA = start Tomcat %JRE_HOME%\bin\java
//JAVA_OPTS = " -Djava.util.logging.config.file="D:\lfy\tools\Apache-tomcat-test\conf\logging.
properties" -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"

//CATALINA_OPTS=""

//DEBUG_OPTS = ""

//CMD_LINE_ARGS = ""

//ACTION = "start"
goto end
:doSecurity
%_EXECJAVA% %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -Djava.endorsed.dirs="%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% %JAVA_OPTS% %CATALINA_OPTS% %JPDA_OPTS% %DEBUG_OPTS% -Djava.endorsed.dirs="%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% %JAVA_OPTS% %CATALINA_OPTS% %JPDA_OPTS% %DEBUG_OPTS% -Djava.endorsed.dirs="%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

 

你可能感兴趣的:(启动tomcat,批处理文件分析)