Parse Tomcat's startup script--catalina.bat_tomcat

Source: Internet
Author: User
Tags goto tomcat

Overview

The three most important startup scripts for Tomcat:

    • Startup.bat
    • Catalina.bat
    • Setclasspath.bat

We analyzed the Startup.bat script in the last article.

Let's analyze the Catalina.bat script in this article.

As for Setclasspath.bat this script, I believe that after reading this, you can read this script.
Click to download [Setclasspath.bat script] to view the annotated Setclasspath.bat script

Catalina.bat

The code for this script is a bit more, and it's a separate piece of content that shows the Catalina.bat script. Click [Catalina.bat script] to download the view.

Let's analyze the contents of the script in one line.

@echo off
setlocal

These two commands refer to the previous article (parse Tomcat's startup script--startup.bat) for explanations

First piece of scripting code

rem Suppress Terminate batch job on CTRL + C
if not ""%1 "" = "" Run "" Goto mainentry
If "%TEMP%" = "" Goto MAINENTR Y
if exist "%temp%\%~nx0.run" goto mainentry
echo y> "%temp%\%~nx0.run"
if not exist "%temp%\%~nx0.run" Goto Mainentry
echo y> "%temp%\%~nx0. Y "Call
"%~f0 "%* <"%temp%\%~nx0. Y "
REM use provided errorlevel
set retval=%errorlevel%
del/q"%temp%\%~nx0. Y ">nul 2>&1
exit/b%retval%
: mainentry del/q
"%temp%\%~nx0.run ">nul 2>&1

The role of the script

Determine if the user is using

catalina.bat run

To start Tomcat's.

If the user starts Tomcat using the Startup.bat script, then the script will not be executed.

The code looks messy and is analyzed slowly.

First line:

Note: It is forbidden to use CTRL + C to terminate the batch task, or how to prohibit it.

Second line:

if not ""%1"" == ""run"" goto mainEntry

First of all, what does this "%1" variable represent here? Normally, this script is called by the Startup.bat script and is invoked with a start parameter passed (previous analysis). %1 in the batch command represents the first argument after the command, which refers to start. So "%1" = start. If the user starts Tomcat with the Catalina.bat Run command, then "%1" = run.

Third line:

if "%TEMP%" == "" goto mainEntry

The%TEMP% here is very likely to be considered empty, in fact it can be read to the system's environment variables. Therefore, the%TEMP% here is the system's environment variable value, usually installed Windows system, the system will automatically configure the environment variable. So there's usually a value here. You can go to the system's environment variable look at it point to that directory, is generally C:\Users\ username \appdata\local\temp. Note: AppData is a hidden directory.

Line four:

if exist "%TEMP%\%~nx0.run" goto mainEntry

Here comes a new thing%~nx0. In the batch, we know that%1 represents the first parameter after the program, then%0 It? %0 Represents the name of the executable program,%~nx0 is the name of the program + extension

Here is the Catalina.bat. You can write a small script (Test.bat) to verify: (My script is placed under D disk)

Script content:

@echo off
echo "%~nx0"
echo "%1"

Execution results:

PS d:\>. \test.bat Hello
"test.bat"
"Hello"
PS d:\>

Line five:

echo Y>"%TEMP%\%~nx0.run"

This code is simply written to the character Y into the%temp%\catalina.bat.run file.

Line Six:

if not exist "%TEMP%\%~nx0.run" goto mainEntry

I also judged if the%temp%\catalina.bat.run file exists.

Line seventh:

echo Y>"%TEMP%\%~nx0.Y"

With line fifth, write Y to%temp%\catalina.bat.y. If the file does not exist, create a new one.

Line eighth:

call "%~f0" %* <"%TEMP%\%~nx0.Y"

This line is a bit interesting. There were two new things:

(Because of Markdown syntax restrictions, write the following code into a block of code)

-"%~f0": simply to represent the absolute path of the current command.
-"%*": We know that%1 represents the first argument, and so on,%2 represents the second .... Then the%* is well understood, representing all the parameters.

Verify

Script content:

@echo off
echo "%*"
echo "%~f0"

Execution results:

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

Then the back of the < "%temp%\%~nx0. Y "means to read the contents of the%temp%\catalina.bat.y file.

And then called by call.

We write our own example, set up Test.bat files in D disk, and then create catalina.bat.y files.

Script content:

call "%~f0" %* < D:/catalina.bat.Y

Catalina.bat.y File Contents

Y

Execution results:

........
D:\>call "D:\test.bat" Hello World 0<d:/catalina.bat.y
d:\>call "D:\test.bat" Hello World 0<d:/ Catalina.bat.y
d:\>call "D:\test.bat" Hello World 0<d:/catalina.bat.y
d:\>call "D:\test.bat" Hello World 0<d:/catalina.bat.y
D:\>call "D:\test.bat, Hello World 0<d:/catalina.bat.y
d:\>call" D:\ Test.bat "Hello World 0<d:/catalina.bat.y
d:\>call" D:\test.bat "Hello World 0<d:/catalina.bat.y
* * * b A T c H R E c U r S I O N exceeds STACK limits
recursion count=593, stack usage=90 percent Hu
Jintao b A T C H processing is a B O R t E D

A lot of duplicate code is omitted from the top, and it is found that it calls itself continuously until the stack limit is exceeded.

If we add @echo off,

@echo off call
"%~f0"%* < D:/catalina.bat.y

The results will only appear

D:\>.\test.bat Hello World
B A T c H R E C U r S I O N exceeds STACK limits
recursion count=593, Stack usage=90 percent
b a T C H processing is a b O R t E D

We just need to understand how these commands work, and we'll later summarize the purpose of Tomcat to execute these commands.

Line Tenth:

set RETVAL=%ERRORLEVEL%

If we know Linux, every command will return an exit code after the execution completes. After Linux executes a command, use echo $? To view the exit code for the previous command. The same is true in Windows, which has its own exit code after the command has been executed. The%errorlevel% here is the exit code for the call command above. Assign value to a variable RETVAL

Line 11th:

del /Q "%TEMP%\%~nx0.Y" >NUL 2>&1

Here comes a del command, which is easy to associate with delete, so what does/q mean? Silently Delete, will not give you any hint, just like rm-f in Linux, here is delete%temp%\catalina.bat.y this file.

What's the meaning of >nul 2>&1 back there?

The principle of redirection of output streams in Linux is the same.

(Because of Markdown syntax restrictions, write the following code into a block of code)

->nul: means to redirect output to NUL, you can't see anything
-2>&1:2: Error output, &1: standard output, which means outputting error messages to standard output.
->nul 2>&1: The error message is first exported to the standard output and then exported to the NUL.

Line 12th:

exit /B %RETVAL%

Exit the current batch,/b specifies the exit number, RETVAL the most exit code, that is, call execution of the command's exit code.

Last two lines:

: Mainentry
del/q "%temp%\%~nx0.run" >nul 2>&1

Define a mainentry label, and then delete the Catalina.bat.run file in the temp directory.

Summarize the functionality of the first script

In short, the function of this code is to call itself, to determine whether a file in the Temp directory exists to avoid a second callback. It's so complicated to write.

The following goes into Tomcat's formal boot process and does not begin to execute the Main method

Second paragraph script code

REM Guess catalina_home if not defined
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
echo the catalina_home environment variable is isn't defined correctly
echo this environment variable is NE Eded to run this program
goto end
: Okhome
rem Copy catalina_base from Catalina_home if not defined
if Not "%catalina_base%" = "" Goto gotbase
Set "Catalina_base=%catalina_home%"
: Gotbase

This script is still relatively simple, mainly set up two environment variables catalina_home and catalina_base.

If you do not configure the Catalina_base environment variable, refer directly to the value of Catalina_home

Calm down to see a little bit of the understanding.

Third paragraph script code

REM Ensure that neither Catalina_home nor Catalina_base contains a semi-colon rem as this was used as the
separator in The Classpath and Java provides no REM mechanism for escaping if the same character in the
path. Check this
rem by replacing all occurrences of ', ' with ' and checking ' neither
rem catalina_home nor Catalin A_base have changed
if "%catalina_home%" = = "%catalina_home:;=%" goto Homenosemicolon
echo Using catalina_home: "%catalina_home%"
Echo Unable to start as Catalina_home contains a semicolon (;) character
goto end
: homenose Micolon
If "%catalina_base%" = "%catalina_base:;=%" goto Basenosemicolon
echo Using catalina_base: "% Catalina_base% "
Echo Unable to start as Catalina_base contains a semicolon (;) character
goto end
: Basenos Emicolon

This is mainly to determine the value of the CATALINA_HOME environment variable and the value of the CATALINA_BASE environment variable to end with a semicolon, if the end of the semicolon, the error exits.

Fourth segment scripting code

REM Ensure that any user defined CLASSPATH variables are does used on startup, REM but allow the to is
them in SE Tenv.bat, in rare case, where it is needed.
Set classpath=
REM Get standard environment variables
if not exist "%catalina_base%\bin\setenv.bat" goto Checksetenvhome call
"%catalina_base%\bin\setenv.bat"
goto setenvdone
: checksetenvhome
if exist "% Catalina_home%\bin\setenv.bat "called"%catalina_home%\bin\setenv.bat "
: Setenvdone
rem Get Standard Java Environment variables
if exist "%catalina_home%\bin\setclasspath.bat" goto Oksetclasspath
Echo Cannot find " %catalina_home%\bin\setclasspath.bat "
echo this file is needed to run"
-"goto end
: o Ksetclasspath call
"%catalina_home%\bin\setclasspath.bat"%1
if errorlevel 1 goto end

Set a temporary environment variable: CLASSPATH.

If the Setnv.bat script exists under Tomcat's Bin directory, execute it. It is not usually the case.

Then judge whether the Setclasspath.bat script exists, if it does not exist, the direct error, stop the start Tomcat.

If it exists, call it and pass the first argument in.

Setclasspath.bat This script mainly sets up several environment variables

    • Java_home
    • Jre_home
    • Java_endorsed_dirs =%catalina_home%\endorsed
    • _runjava =%jre_home%\bin\java.exe
    • _runjdb =%java_home%\bin\jdb.exe

Fifth segment scripting code

REM ADD on extra jar file to CLASSPATH
REM Note that there are no quotes as we don't want to introduce
REM Quotes into the CLASSPATH
if%classpath% "= =" goto Emptyclasspath
Set "classpath=%classpath%"
: Emptyclasspath
Set "Classpath=%classpath%%catalina_home%\bin\bootstrap.jar"
if not "%catalina_tmpdir%" = = "Goto Gottmpdir
Set" Catalina_tmpdir=%catalina_base%\temp "
: Gottmpdir
rem ADD Tomcat-juli.jar to Classpath
rem Tomcat-juli.jar can over-ridden per instance
if not exist "%catalina_base%\bin\ Tomcat-juli.jar "goto juliclasspathhome
Set" Classpath=%classpath%;%catalina_base%\bin\tomcat-juli.jar "
goto juliclasspathdone
: Juliclasspathhome
Set "classpath=%classpath%;%catalina_home%\bin\ Tomcat-juli.jar "
: Juliclasspathdone

This code basically does three things:

    • Add the Bootstrap.jar in the Tomcat Bin directory to environment variables
    • Set the value of the CATALINA_TMPDIR environment variable to the TEMP directory under the Tomcat directory
    • Add the Tomcat-juli.jar in the Tomcat Bin directory to environment variables

Sixth segment Scripting code

If not "%jsse_opts%" = "" goto gotjsseopts
Set jsse_opts= "-djdk.tls.ephemeraldhkeysize=2048"
: gotjsseopts
Set "java_opts=%java_opts%%jsse_opts%"
rem Register custom URL handlers rem Do-so
custom URL Handl ES (specifically ' War: ... ') can be used into the security policy
set "java_opts=%java_opts%-djava.protocol.handler.pkg S=org.apache.catalina.webresources "
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% "
if not"%logging_manager% "= =" goto Nojulimanager
Set logging_manager=- Djava.util.logging.manager=org.apache.juli.classloaderlogmanager
: Nojulimanager
Set "JAVA_OPTS=%JAVA_ opts%%logging_manager% "

The main is to append a series of startup parameters to java_opts this environment variable.

Eighth Segment scripting code

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%"

The main is to print related environment variable information.

Nineth Segment Scripting code

Set _execjava=%_runjava%
set Mainclass=org.apache.catalina.startup.bootstrap
set Action=start
set security_policy_file=
set debug_opts=
set jpda=

Set the environment variables for some columns:

    • _runjava:%jre_home%\bin\java.exe
    • MainClass: Tomcat's startup class is specified, and the main method is within this class.
    • Action: Motion: is to start
    • Security_policy_file: Security policy File, if the-security parameter is added at startup, this parameter is assigned to the Catalina.policy file in Tomcat's conf directory.
    • JPDA: This parameter can be Baidu, we usually hardly use.

Tenth paragraph Code

If not "%1" "= =" "Jpda" "Goto NOJPDA
Set JPDA=JPDA
if not"%jpda_transport% "= =" goto Gotjpdatransport
Set Jpda_transport=dt_socket
: Gotjpdatransport
if not "%jpda_address%" = "" goto gotjpdaaddress
Set Jpda_ address=localhost:8000
: gotjpdaaddress
if not "%jpda_suspend%" = "" goto gotjpdasuspend
Set Jpda_ Suspend=n
: 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

Seems to jump directly from the first line to the last line, yes, generally I did not start the time without the JPDA parameters, here will skip directly, inside the script is about the JPDA settings.

11th Segment Scripting code

If ""%1 "" = = "" Debug "" Goto Dodebug
If ""%1 "" = "" Run "" Goto Dorun
If ""%1 "" = = "" Start "" Goto Dostart
If ""% 1 "" = = "" Stop "" Goto dostop
If ""%1 "" = "" Configtest "" Goto doconfigtest
If ""%1 "" = = "" Version "" Goto doversion< C7/>echo Usage:catalina (Commands ...)
echo Commands:
echo Debug Start Catalina in a debugger
echo debug-security debug Catalina with a security manage  R
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 windows with Security Manager
Echo stop stop Catalina
echo configtest Run A Basic syntax check on Server.xml
echo version What version of Tomcat are for you running?
Goto END

It seems to be a switch.

    • If we start Tomcat with Startup.bat, the value of "%1" here is start
    • If Tomcat is started with Catalina.bat run, the value of '%1 ' here is run

12th Segment Scripting code

:D Orun
Shift if not ""
%1 "= =" "-security" goto execcmd
shift
echo Using security Manager
Set " Security_policy_file=%catalina_base%\conf\catalina.policy "
goto execcmd
:d ostart
shift
if"% title% "= =" "Set Title=tomcat
set _execjava=start"%title% "%_runjava%
if not" "%1" "= =" "-security" "Goto exec CMD
shift
echo Using security Manager
Set "security_policy_file=%catalina_base%\conf\ Catalina.policy "
Goto ExecCmd

First, analyze the two shift commands

The first shift is to remove the start or run parameter, and then use "%1" to fetch the argument, which is the second in the argument list.

The second shift is removed in the second parameter.

Let's compare the startup difference between start and run.

Difference

If "%title%" = "" Set Title=tomcat
set _execjava=start "%title%"%_runjava%
    • If the Startup.bat script starts, a new CMD window will be started, and the title of CMD is set to Tomcat.
    • If it is Catalina.bat run startup, will not create a new CMD window, and will not set CMD title.

Finally all jumped to the ExecCmd label.

13th Segment Scripting code

: 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

Here, "%1" is used to remove the arguments after the start command, and if so, append to the CMD_LINE_ARGS environment variable and remove the parameter.

Normally, we don't have any arguments here,-security we don't append this parameter.

Continue to go down.

14th Segment Scripting code

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%
Goto End

Obviously, our%jpda% has no value and does not jump; Since we do not have-security parameters,%security_policy_file% has no value and does not jump.

The following section-length command is to start the BootStrap class and pass the corresponding arguments in.

The contents of this long command can be parsed as long as the corresponding environment variables are replaced with their values. Believe you can. Be patient!

To sum up

    1. First, judge the user to start the Tocmat directly using Catalina.bat run
    2. Setting Catalina_home and catalina_base environment variable values
    3. Verifying the correctness of catalina_home and catalina_base environment variable values
    4. Calling the Setnv.bat script
    5. Calling the Setclasspath.bat script
    6. Add Bootstrap.jar and Tomcat-juli.jar to CLASSPATH
    7. Set the value of the Catalina_tmpdir temp directory to the temp in the Tomcat directory
    8. Append a series of parameters to the Java_opts
    9. Integration of related startup information, parameters
    10. Start Tomcat

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring some help, interested friends can see the next article "Parsing tomcat startup script-startup.bat"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.