Tomcat Start analysis (why do we configure Catalina_home environment variables)

Source: Internet
Author: User
Tags goto parent directory tomcat server

Original address: http://www.cnblogs.com/heshan664754022/archive/2013/03/27/2984357.html


Open the batch file used to start Tomcat by using the text Editing tool Startup.bat, read it carefully. In this file, you first determine whether the CATALINA_HOME environment variable is empty, and if it is empty, set the current directory to the Catalina_home value. 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 Catalina_home value. According to 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 invoked through this environment variable. Through this analysis, we learned two information, one is Tomcat start, need to find catalina_home this environment variable, if in Tomcat's Bin directory down with Startup.bat,tomcat will 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 call Startup.bat when we are not in the Tomcat's Bin directory as the current directory, the error message shown in the following figure appears (except in the parent directory of the bin directory).

Figure error starting tomcat in other directory

If you want to start Tomcat in any directory, you need to set the CATALINA_HOME environment variable, you can add Catalina_home to the Windows XP system environment variable, the value is Tomcat's installation directory. On the author's machine, the Tomcat installation directory is D:\apache-tomcat-6.0.36-windows-x86\apache-tomcat-6.0.36. The process of adding a catalina_home environment variable is the same as the process of adding a java_home environment variable as described earlier. If you do not want to add to the system's environment variables, you can also set it directly in the Startup.bat file. The following is a fragment of the file 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_home=%current_dir%"
If exist "%catalina_home%\bin\catalina.bat" goto okhome
Cd..

...

Note that this code, shown in bold, is used to set the CATALINA_HOME environment variable, and below it is a statement that determines whether the catalina_home is empty. If you don't find a location, simply place the code that sets the CATALINA_HOME environment variable on 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 Tomcat servers in other directories, you will need to set both the Catalina_home and JAVA_HOME environment variables in the Shutdown.bat file. The position of the variable is the same as the Startup.bat file, before you can determine whether the catalina_home is empty. Of course, for once and for all, to avoid having to reload Tomcat and to set it up (you need to have the same version of Tomcat installed in the same location), we'd better add catalina_home and java_home environment variables to the Windows XP system's environment variables.

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

Under Windows systems, the name of an environment variable is not case-insensitive, that is, Java_home and Java_home are the same.

With the Startup.bat file in view, let's take a look at the Catalina.bat file that is really responsible for starting the Tomcat server. By analyzing the Catalina.bat file, we found that it also called a file setclasspath.bat. In the Setclasspath.bat file, it checks for the existence of the JAVA_HOME environment variable and, by java_home the environment variable, finds Java.exe to start Tomcat. In this file, other variables are set up, representing the standard commands for invoking Java, and interested readers can analyze the file themselves. After the Setclasspath.bat has been executed, the remaining portion of Catalina.bat begins the Tomcat server's startup process.

When you execute catalina.bat directly, you need to take the arguments on the command line. The reader can perform catalina.bat at the command Prompt window and print out the various parameters and meanings of the Catalina.bat command, as shown in the following figure.

(Click to view larger image) Catalina.bat information of each parameter

The commonly used parameters are start, run, and stop. Parameter start indicates that the Tomcat server is started in a separate window, parameter run indicates that the Tomcat server is started in the current window, and the parameter stop indicates that the Tomcat server is turned off. We execute the Startup.bat, which actually executes the "catalina.bat start" command, and executes Shutdown.bat, which actually executes the "Catalina.bat stop" command. The "Catalina.bat Run" command is sometimes very useful, especially when we need to view the Tomcat error message.

When you develop a JSP program, you often encounter a 8080-port number on your machine that is occupied by another application, or when you configure Server.xml with an error, when passing Startup.bat (equivalent to executing "catalina.bat start" When starting a Tomcat server, if a critical error occurs during startup, because the Tomcat server is started in a separate window, the command Prompt window shuts down automatically when the startup fails, and the error messages that are output from running the program disappear with no log information. This leaves us no way to find out the cause of the mistake. When an error occurs, we can switch to the "catalina.bat Run" command to start again, and once the boot fails, only the Tomcat server terminates abnormally, but the error message at startup is still preserved under the current Command Prompt window, so we can find out why the startup failed.

This article has been transferred from Sun Xin's "servlet/jsp in-depth detailed: Web development based on Tomcat"

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.