Parse the Tomcat startup script startup.bat
- 2020-05-30 21:24:25
- OfStack
An overview of the
We usually use startup. bat in Tomcat to start Tomcat. But what does this do?
You all know that if an Java program needs to be started, it definitely needs the main method, so where is the main method?
What parameters are configured in the Tomcat script, and when will Tomcat fail to start?
Let's take a look at Tomcat's three most important startup scripts with a list of questions:
startup.bat catalina.bat setclasspath.batstartup bat script
The script does the following things:
Set the value of the CATALINA_HOME environment variable Find the catalina.bat script Call the catalina.bat script and pass the argumentsPost the content of the simplified version of the startup.bat script
@echo off
rem After executing this command , Environment variables are added or changed only if they match to endlocal Command or go to the end of the file .
setlocal
rem Assuming that CATALINA_HOME Environment variables are not defined
rem Takes the path value of the current directory , Assigned to CURRENT_DIR variable , is ./apache-tomcat-x.x.xx/bin
set "CURRENT_DIR=%cd%"
rem if CATALINA_HOME The variable value is not "" if , Transferred to the gotHome Labels place
if not "%CATALINA_HOME%" == "" goto gotHome
rem if CATALINA_HOME is "" if , Set up the CATALINA_HOME A variable's value to The path value of the current directory (./apache-tomcat-x.x.xx/bin)
set "CATALINA_HOME=%CURRENT_DIR%"
rem Determine if there are any in the current path bin\catalina.bat, That is ./apache-tomcat-x.x.xx/bin/bin/catalina.bat
rem If it exists , Directly transferred to the okHome Labels place , Obviously it doesn't exist
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
rem If it doesn't exist , CATALINA_HOME Takes the value of the parent directory , That is (./apache-tomcat-x.x.xx/)
cd ..
set "CATALINA_HOME=%cd%"
rem Enter the CURRENT_DIR(./apache-tomcat-x.x.xx/bin)
cd "%CURRENT_DIR%"
:gotHome
rem Go through the Settings above , CATALINA_HOME The value of theta is already zero : ./apache-tomcat-x.x.xx/
rem So the collation can be found catalina.bat The script , Directly transferred to the okHome Labels place
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 Set up the EXECUTABLE The variable points to catalina.bat The script
set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat"
rem Check the target executable (catalina.bat) If there is a , Usually it does , Directly transferred to the okExec Labels place
rem If it doesn't exist , Directly out of . Start the Tomcat The end of the
if exist "%EXECUTABLE%" goto okExec
echo Cannot find "%EXECUTABLE%"
echo This file is needed to run this program
goto end
:okExec
rem It's no use getting the rest shift The command line argument that is fetched , And save them in CMD_LINE_ARGS
set CMD_LINE_ARGS=
:setArgs
rem If the first 1 Three command line arguments are empty , Jump to the doneSetArgs Labels place
rem "%1" : Represents the first after the execution of the command 1 A parameter
if ""%1""=="""" goto doneSetArgs
rem The first 1 If the parameter is not empty , Joining together to CMD_LINE_ARGS variable
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
rem This command can be baidu
shift
goto setArgs
:doneSetArgs
rem It's set up EXECUTABLE The value of the variable is pointing catalina.bat The script , The use of call Command execution call , And pass in the parameters
rem The following , Let's see catalina.bat Script content
rem Complete command : ./apache-tomcat-x.x.xx/bin/catalina.bat start
call "%EXECUTABLE%" start %CMD_LINE_ARGS%
:end
To understand some of the commands in the script, take a look at the following common commands (Window we used)
rem: the code after this command will not be executed, equivalent to a comment echo off: turn off the display of commands. If not set, any commands executed will be displayed echo: outputs what follows setlocal: after executing this command, the scope of environment variables added or changed is limited to matching the endlocal command or reaching the end of the file. set: sets 1 variable :xxx: define 1 tag goto: jump to the designated label call: execute the commandLet's take a one-line look at the startup.bat script
set "CURRENT_DIR=%cd%"
%cd% : represents the path to the directory where the file is located
If we extracted Tomcat directory for D: / apache - tomcat - x. x. x /. Because startup. bat command in bin directory, so the % % cd said directory is D: / apache - tomcat - x. x. x/bin
if not "%CATALINA_HOME%" == "" goto gotHome
We don't normally configure the CATALINA_HOME environment variable, so we won't go to the gotHome TAB.
set "CATALINA_HOME=%CURRENT_DIR%"
Directly assume the current directory to be the value of CATALINA_HOME
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
Then fixed format to determine whether one have catalina bat script, of course, this is will not exist, because CATALINA_HOME = D: / apache - tomcat - x. x. x/bin
cd ..
set "CATALINA_HOME=%cd%"
Because the Tomcat directory format is fixed, here you go directly to the parent directory (cd..) , and then set the value of CATALINA_HOME to the parent directory (D:/ apache-tomcat-x.x.x).
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
If we unzip Tomcat and put startup.bat in the bin directory that is not Tomcat, we can't find it here, so we can directly goto end and exit the startup of Tomcat.
Ok, so here we go straight to the okHome TAB.
:okHome
set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat"
Ok, it's easy to set the value of the 1 EXECUTABLE variable to point to the catalina.bat script.
if exist "%EXECUTABLE%" goto okExec
echo Cannot find "%EXECUTABLE%"
echo This file is needed to run this program
goto end
Once again, I checked whether this script exists or not. If it does, I will directly transfer it to the okExec label and it can be executed.
If it does not pass the check, exit the boot and print an error message.
:okExec
set CMD_LINE_ARGS=
:setArgs
if ""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
shift
goto setArgs
A variable CMD_LINE_ARGS is set, and its value is temporarily empty
There appeared a "" % 1" = = "" "" "", open it to see is whether" % 1 "is equal to". "well, what does" % 1 "is the & # 63;
This is a syntax for the window batch, which is the first argument after the command is executed. For this case, we did not pass any arguments, so the '%1' is' "(empty).
Jump directly to the doneSetArgs label.
If it's not empty, put it in the back.
The shift command in this case means to remove one parameter. For example:
@echo off
echo "%1"
shift
echo "%1"
Build an test.bat batch, copy the above code in, execute it in cmd and give it two parameters
Here is the result of the execution, where you can remove @echo off and execute it to verify that the command 1 is working
PS D:\> .\test Hello World
"Hello"
"World"
PS D:\>
So that should make sense to you.
Continue to analyze
:doneSetArgs
call "%EXECUTABLE%" start %CMD_LINE_ARGS%
:end
EXECUTABLE = %CATALINA_HOME%\bin\ catalina.bat is set above, so the script catalina.bat is actually called, and an start parameter is passed to it.
If we run startup.bat in cmd and we have a parameter of 1, then 1 is passed.
This is actually done: %CATALINA_HOME%\bin\ catalina.bat start
conclusion
The purpose of this script is to find catalina.bat and call it.
The above is the whole content of this article, I hope the content of this article can bring you a certain help in your study or work, the next article will continue to introduce Tomcat related knowledge -- "parsing Tomcat's startup script --catalina.bat", interested friends can read it