Tomcat Boot analysis (why do we configure the CATALINA_HOME environment variable)

Source: Internet
Author: User

Use the text Editing tool to open the batch file used to start Tomcat Startup.bat, read it carefully. In this file, first determine whether the CATALINA_HOME environment variable is empty, and if it is empty, set the current directory to the value of Catalina_home. Then determine if there is a bin\catalina.bat in the current directory, and if the file does not exist, set the parent directory of the current directory to the value of Catalina_home. Based on the hierarchy of the Tomcat installation directory on the author's machine, the last Catalina_home value is set to the Tomcat installation directory. If the environment variable catalina_home already exists, the "catalina.bat start" command under the Bin directory is called through this environment variable. Through this analysis, we learned two information, one is Tomcat boot, need to find catalina_home this environment variable, if the Startup.bat,tomcat in Tomcat Bin directory is automatically and correctly set Catalina_home The second is to execute the startup.bat command, which actually executes the "catalina.bat start" command.

If we do not call Startup.bat in the Tomcat bin directory as the current directory, the error message as shown (except for the parent directory of the bin directory is lowered).

Figure starting Tomcat error in other directories

If you want to start Tomcat in any directory, you need to set the CATALINA_HOME environment variable, and you can add catalina_home to the environment variable of your Windows XP system, whose value is the installation directory of Tomcat. On the author's machine, Tomcat's installation directory is D:\apache-tomcat-6.0.36-windows-x86\apache-tomcat-6.0.36. The process of adding catalina_home environment variables is the same as the procedure for adding java_home environment variables. If you do not want to add the environment variables in the system, you can also set them directly in the Startup.bat file. Here is the file fragment after setting catalina_home in the Startup.bat file:

REM $Id: Startup.bat 908749 2010-02-10 23:26:42z Markt $ rem---------------------------------------------------------- -----------------

Set catalina_home=d:\apache-tomcat-6.0.36-windows-x86\apache-tomcat-6.0.36

REM Guess catalina_home if not defined set "current_dir=%cd%" If not "%catalina_home%" = = "goto gothome Set" Catalina_ho me=%current_dir% "if exist"%catalina_home%\bin\catalina.bat "goto okhome CD.

...

Note that the code shown in bold is to set the CATALINA_HOME environment variable, and below it is the statement that determines whether the catalina_home is empty. If you can't find a location, simply place the code that sets the CATALINA_HOME environment variable in the first line of the file. Java_home environment variables can also be set in the same way. However, if you want to use Shutdown.bat to shut down the Tomcat server in a different directory, you need to set the environment variables Catalina_home and java_home in the Shutdown.bat file. The position of the set variable is the same as the Startup.bat file, before judging whether the catalina_home is empty. Of course, for once and for all, to avoid having to set up after you reinstall Tomcat (you need to have the same version of Tomcat installed in the same location), we'd better add both the catalina_home and JAVA_HOME environment variables to the environment variables of the Windows XP system.

Some readers may be surprised that the name of the environment variable of the Tomcat installation directory is catalina_home and, according to the settings of other environment variables, Java_home represents the installation directory of the JDK, then the installation directory of Tomcat should be represented by Tomcat_home. , but why use Catalina_home? In fact, before Tomcat 4, Tomcat_home was used to represent the installation directory of Tomcat, and after Tomcat 4, the new servlet container Catalina was used, so the name of the environment variable was changed to Catalina_home.

The name of the environment variable under the Windows system is not case-insensitive, that is, Java_home and Java_home are the same.

Once you know the Startup.bat file, let's take a look at the Catalina.bat file that is really responsible for starting the Tomcat server. By parsing the Catalina.bat file, we found that it also called a file setclasspath.bat. In the Setclasspath.bat file, it checks to see if the JAVA_HOME environment variable exists and, through Java_home environment variables, finds Java.exe, which is used to start Tomcat. In this file, other variables are set, representing standard commands for calling Java, and interested readers can analyze the file on their own. After executing the setclasspath.bat, the rest of Catalina.bat started the Tomcat server startup process.

When executing catalina.bat directly, you need to bring the command line arguments. The reader can execute catalina.bat at the command Prompt window and print out the various parameters of the Catalina.bat command and their meanings, as shown in.

(Click to view larger image) Figure Catalina.bat parameters Information

The commonly used parameters are start, run, and stop. The parameter start means that the Tomcat server is started in a separate window, the parameter run indicates that the Tomcat server is started in the current window, and the parameter stop indicates that the Tomcat server is shut down. We execute the Startup.bat, actually execute the "catalina.bat start" command, execute the Shutdown.bat, and actually execute the "catalina.bat stop" command. The "Catalina.bat Run" command is sometimes very useful, especially if we need to look at Tomcat's error message.

When developing a JSP program, you often encounter a 8080 port number on your machine that is being used by another application, or an error occurred while configuring Server.xml, when passing startup.bat (equivalent to executing "catalina.bat start" When you start the Tomcat server, if a critical error occurs during startup, because the Tomcat server is started in a separate window, the command Prompt window automatically shuts down once the boot fails, and the error message that is output from the program runs disappears with no log information. This makes it possible for us not to find the cause of the error. When an error occurs, we can switch to "catalina.bat Run" command to start again, once the boot fails, only the Tomcat server terminates abnormally, but at the current command prompt window still retains the startup error message, so that we can find the reason for the startup failure.

Tomcat Boot analysis (why do we configure the CATALINA_HOME environment variable)

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.