Parse Tomcat's startup script--startup.bat_tomcat

Source: Internet
Author: User
Tags goto parent directory

Overview

We usually use the Startup.bat in Tomcat to start Tomcat. But what has been done about it?

We all know that a Java program needs to start, you definitely need a main method, then where is this main method?

What parameters are configured in the Tomcat script, and under what circumstances will tomcat fail to start?

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

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

Startup.bat Script

The script mainly does the following things:

    • Set the value of the CATALINA_HOME environment variable
    • Find Catalina.bat Script
    • Call the Catalina.bat script and pass the arguments over

Post the contents of a simplified version of the Startup.bat script

@echo off rem After executing this command, the added or altered environment variable is limited to matching to the endlocal command or to the end of the file. Setlocal REM assumes that the CATALINA_HOME environment variable does not define REM takes the path value of the current directory and assigns it to the Current_dir variable, which is./apache-tomcat-x.x.xx/bin Set "current_dir=%c  d% "REM If Catalina_home variable value is not" ", tune to Gothome label if not"%catalina_home% "=" "goto gothome REM If Catalina_home is" " , set the Catalina_home variable value to the current directory's Path value (./apache-tomcat-x.x.xx/bin) Set "catalina_home=%current_dir%" REM to determine if there is a bin\ under the current path Catalina.bat, which is./apache-tomcat-x.x.xx/bin/bin/catalina.bat REM, if present, is directly transferred to the Okhome label, apparently non-existent if exist "%catalina_ho
Me%\bin\catalina.bat "Goto okhome REM does not exist, Catalina_home takes the value of the parent directory, i.e. (./apache-tomcat-x.x.xx/) CD. Set "catalina_home=%cd%" Rem enters Current_dir (./apache-tomcat-x.x.xx/bin) CD "%current_dir%": Gothome REM through the above settings, The value of Catalina_home is already:./apache-tomcat-x.x.xx/rem so collation can be found Catalina.bat script, directly to the okhome tag if exist "%catalina_home%\bin" \catalina.bat "Goto okhome echo the catalina_home environment variable isn't defined correctly echo this EnvironmeNT variable is needed to run this program goto end:okhome REM Set executable variable to point to Catalina.bat script set "Executable=%catali Na_home%\bin\catalina.bat "REM examines whether the target executable (Catalina.bat) exists and is normally present, directly to the Okexec label REM if it does not exist, exit directly. Start Tomcat End If exist "%executable%" goto okexec Echo Cannot find "%executable%" echo this file are needed to run this prog  Ram Goto end:okexec REM Gets the remaining command line arguments that are not taken out with shift and saves them in Cmd_line_args set cmd_line_args=: Setargs REM If the first command-line argument is empty, skip to The Donesetargs label is rem "%1": Indicates the first argument after executing the command if "%1" "= =" "" Goto Donesetargs rem First parameter is not empty, splice to Cmd_line_args variable set cmd_l ine_args=%cmd_line_args%%1 rem This command can own the Baidu shift goto Setargs:d Onesetargs REM The value of the executable variable was set to point to the Catalina.bat script, This invokes the call command and passes the parameters into rem next, let's look at the contents of the Catalina.bat script 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, first take a look at the common commands (we use the Window version)

    • REM: The code after this command will not be executed, equivalent to the annotation
    • @echo off: The display of the command is turned off, and if there are no settings, the commands that are executed will be displayed
    • Echo: What's behind the output
    • Setlocal: After executing this command, the scope of the added or altered environment variable is limited to matching to the endlocal command or to the end of the file.
    • Set: Sets a variable
    • : XXX: Define a label
    • Goto: Jump to the established label
    • Call: Execute command

We're going to do a line analysis Startup.bat script

set "CURRENT_DIR=%cd%"

%cd%: The path to the directory where the file resides

If the directory where we unzipped the Tomcat is d:/apache-tomcat-x.x.x/. Because the Startup.bat command is in the bin directory, the directory represented at this time%cd% is D:/apache-tomcat-x.x.x/bin

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

We do not normally configure Catalina_home this environment variable, so this will not be transferred to the Gothome tab.

set "CATALINA_HOME=%CURRENT_DIR%"

Directly assume the current directory as Catalina_home value

if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome

And then to determine whether there is a catalina.bat script in a fixed format, of course, this is certainly not there, because catalina_home = D:/apache-tomcat-x.x.x/bin

CD..
Set "catalina_home=%cd%"

Because Tomcat's catalog format is fixed, it goes directly to the parent directory (CD ...) and then sets the Catalina_home value 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 Cor rectly
echo This environment variable be needed to run this program
Goto end

Continue to look down, here again to determine if catalina.bat in such a directory structure can be found, if we extract Tomcat after the Startup.bat in the non-Tomcat bin directory, this is not found here, directly goto E nd, quit Tomcat's boot.

OK, here we go directly to the okhome tag.

: Okhome
Set "Executable=%catalina_home%\bin\catalina.bat"

Okay, here's the simple, set the value of a executable variable to point to the Catalina.bat script.

If exist "%executable%" goto okexec
Echo Cannot find "%executable%" The
echo this file was needed to run this program
   goto End

Once again check the existence of the script, if there is, directly to the okexec tag, you can execute.

If the check is not passed, the boot is still exited and the error message is 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

There is a "%1" "= =" "" ", which is to determine whether"%1 "equals" ". So what is "%1"?

This is a syntax for the window batch that represents the first argument after executing the command, and for this we have not passed any arguments, so the "%1" Here is "" (empty).

Jump directly to the Donesetargs tab.

If it's not empty, spell it in the back.

Here's what this shift command means to remove an argument, for example:

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

Build a test.bat batch program, then copy the code above, 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 the function of this command.

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

In this way, we should be able to understand.

Continue to analyze

:d Onesetargs called
"%executable%" Start%cmd_line_args%
: End

The executable =%catalina_home%\bin\catalina.bat is set above, so this is actually called the Catalina.bat script and then passes a start argument to it.

If we run Startup.bat in cmd and follow some parameters, this is passed along.

This is actually done:%catalina_home%\bin\catalina.bat start

Summarize

The script is simple enough to find the Catalina.bat and call it.

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, the next chapter continues to introduce Tomcat related knowledge-"Parse Tomcat startup script--catalina.bat", interested friends can see

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.