SoFunction
Updated on 2025-03-08

Parse Tomcat's startup script--

Overview

We usually use Tomcat to start Tomcat. But what are the things that do?

Everyone knows that if a Java program needs to be started, it must be a main method. So where is the main method?

What parameters are configured in the Tomcat script? Under what circumstances will Tomcat fail to start?

With some questions, let's analyze the three most important startup scripts of Tomcat:

script

This script mainly does the following:

  • Set the value of the CATALINA_HOME environment variable
  • Find the script
  • Call the script and pass the parameters over

Post a simplified version of the script

@echo off
rem After executing this command, Added or changed environment variables are limited to match endlocal Command or reach the end of the file.
setlocal
rem Assumptions CATALINA_HOME Environment variables are not defined
rem Take the path value of the current directory, Give to CURRENT_DIR variable, that is .//bin
set "CURRENT_DIR=%cd%"
 rem if the CATALINA_HOME variable value is not "" If so, adjust to getHome tag
 if not "%CATALINA_HOME%" == "" goto gotHome
 rem if CATALINA_HOME is "" Word, set up CATALINA_HOME variable值为 The path value of the current directory(.//bin)
set "CATALINA_HOME=%CURRENT_DIR%"
 rem determines whether there is bin\ in the current path, that is.//bin/bin/
 If rem exists, it will be directly adjusted to the okHome tag, which obviously does not exist
 if exist "%CATALINA_HOME%\bin\" goto okHome
rem 不存在Word, CATALINA_HOME Take the value of the previous directory, 也that is(.//)
cd ..
set "CATALINA_HOME=%cd%"
rem Enter CURRENT_DIR(.//bin)
cd "%CURRENT_DIR%"
:gotHome
rem 通过上面的set up, CATALINA_HOME The value is already: .//
rem So you can find it  Scripted, Adjust directly to okHome Tags
if exist "%CATALINA_HOME%\bin\" 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 EXECUTABLE variable指向为  script
set "EXECUTABLE=%CATALINA_HOME%\bin\"
rem Check the target executable file()Does it exist, Usually it exists, Adjust directly to okExec Tags
rem 如果不存在Word, Exit directly. start up Tomcat Finish
if exist "%EXECUTABLE%" goto okExec
echo Cannot find "%EXECUTABLE%"
echo This file is needed to run this program
goto end
:okExec
rem Getting the rest is useless shift The command line parameters retrieved, and save them in CMD_LINE_ARGS
set CMD_LINE_ARGS=
:setArgs
rem 如果第一个命令行参数是空Word, Jump to doneSetArgs Tags
rem "%1" : Indicates the first parameter after executing the command
if ""%1""=="""" goto doneSetArgs
rem 第一个参数不是空Word, Spliced ​​to CMD_LINE_ARGS variable
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
rem This command can be used for Baidu
shift
goto setArgs
:doneSetArgs
rem 上面set up了 EXECUTABLE variable的值是指向了  script, This utilization call Command execution call, And pass the parameters in
rem Next, Let's see  Scripted内容
rem Complete command: .//bin/ start
call "%EXECUTABLE%" start %CMD_LINE_ARGS%
:end

To understand some commands in scripts, first let’s understand the commonly used commands (the Window version we use)

  • rem: The code after this command will not be executed, which is equivalent to a comment
  • @echo off: Turn off the display of commands. If there is no setting, any commands executed will be displayed.
  • echo: output the following content
  • setlocal: After executing this command, the scope of the added or changed environment variables is limited to matching the endlocal command or reaching the end of the file.
  • set: Set a variable
  • :xxx: Define a tag
  • goto: Jump to the specified tag
  • call: execute command

Let's analyze the script one by one

set "CURRENT_DIR=%cd%"

%cd%: The path to the directory where the file is located

If the directory where the Tomcat we decompressed is D:// . Because the command is in the bin directory, the directory represented by %cd% at this time is D://bin

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

Under normal circumstances, we will not configure the environment variable CATALINA_HOME, so we will not adjust to the getHome tag here.

set "CATALINA_HOME=%CURRENT_DIR%"

Directly assume the current directory as the value of CATALINA_HOME

if exist "%CATALINA_HOME%\bin\" goto okHome

Then use a fixed format to determine whether there is a script. Of course, it will definitely not exist here, because CATALINA_HOME = D://bin

cd ..
set "CATALINA_HOME=%cd%"

Because the directory format of Tomcat is fixed, we go directly to the upper directory (cd..), and then set the value of CATALINA_HOME to the upper directory (D:/).

if exist "%CATALINA_HOME%\bin\" goto okHome
echo The CATALINA_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program
goto end

Keep reading, and I once again judged whether this directory structure can be found. If we decompress Tomcat and place it in a non-Tomcat bin directory, it cannot be found here, so goto end and exit the startup of Tomcat.

OK, here we will directly adjust to the okHome tag.

:okHome
set "EXECUTABLE=%CATALINA_HOME%\bin\"

OK, here is very simple, set the value of an EXECUTABLE variable to point to the script.

if exist "%EXECUTABLE%" goto okExec
echo Cannot find "%EXECUTABLE%"
echo This file is needed to run this program
goto end

I checked again whether this script exists. If it exists, I can directly click on the okExec tag and can be executed.

If the check fails, the startup will still be exited and the error message will be printed.

:okExec
set CMD_LINE_ARGS=
:setArgs
if ""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
shift
goto setArgs

A CMD_LINE_ARGS variable is set first, and its value is temporarily empty

Here is a ""%1""=="""" which is to tell whether "%1" is equal to "". So what is "%1"?

This is a syntax for window batch processing, which represents the first parameter after executing the command. For this, we do not pass any parameters, so "%1" here is "" (empty).

Jump directly to the doneSetArgs tag.

If it is not empty, just put it in the back.

The shift command here means removing a parameter, and you will know it with an example:

@echo off
echo "%1"
shift
echo "%1"

Create a batch program, then copy the above code, execute it in cmd and give it two parameters

Below is the execution result. Here you can remove @echo off and then execute it to verify the function of this command

PS D:\> .\test Hello World
"Hello"
"World"
PS D:\>

In this way, everyone should be able to understand.

Continue to analyze

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

EXECUTABLE = %CATALINA_HOME%\bin\ is set above, so here is actually calling the script, and then passing a start parameter to it.

If we run in cmd and follow some parameters, we will pass it here together.

This is actually executed: %CATALINA_HOME%\bin\ start

Summarize

This script is quite simple, and its purpose is to find and call it.

The above is the entire content of this article. I hope that the content of this article will be of some help to everyone's study or work. The next article will continue to introduce Tomcat related knowledge--"Parse Tomcat's startup script--》, interested friends can read it