(Turn) Tomcat startup analysis (why should we configure the CATALINA_HOME environment variable)

 

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

 

     Open the batch file startup.bat used to start Tomcat with a text editor and read it carefully. In this file, first determine whether the CATALINA_HOME environment variable is empty, if it is empty, set the current directory to the value of CATALINA_HOME. Then judge whether bin\catalina.bat exists in the current directory. If the file does not exist, set the parent directory of the current directory to the value of CATALINA_HOME. According to the hierarchical structure of the Tomcat installation directory on the author's machine, the value of CATALINA_HOME is finally set to the Tomcat installation directory. If the environment variable CATALINA_HOME already exists, call the "catalina.bat start" command in the bin directory through this environment variable. Through this analysis, we learned two pieces of information. First, when Tomcat starts, you need to find the environment variable CATALINA_HOME. If you call startup.bat in Tomcat's bin directory, Tomcat will automatically and correctly set CATALINA_HOME; the second is to execute startup. The bat command actually executes the "catalina.bat start" command.

 

If we do not call startup.bat when Tomcat's bin directory is the current directory, the error message shown in the following figure will appear (except when it is called in the parent directory of the bin directory).

 technology sharing

Figure Error in starting Tomcat in another 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 environment variable of the Windows XP system, and its value is the installation directory of Tomcat. On the author's machine, the installation directory of Tomcat is D:\apache-tomcat-6.0.36-windows-x86\apache-tomcat-6.0.36. The process of adding the CATALINA_HOME environment variable is the same as the previous process of adding the JAVA_HOME environment variable. If you don't want to add it in the system's environment variables, you can also set it directly in the startup.bat file. Here is the file snippet 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 the function of the code shown in bold is to set the CATALINA_HOME environment variable, and below it is the statement to judge whether CATALINA_HOME is empty. If you can't find the exact location, simply put the code that sets the CATALINA_HOME environment variable on the first line of the file. The JAVA_HOME environment variable can also be set in the same way. However, if you want to use shutdown.bat to shut down the Tomcat server in other directories, you need to set the two environment variables CATALINA_HOME and JAVA_HOME in the shutdown.bat file. The location of the variables is the same as that of the startup.bat file. before CATALINA_HOME is empty. Of course, in order to do it once and for all to avoid setting after reinstalling Tomcat (the same version of Tomcat needs to be installed in the same location), we'd better add the two environment variables CATALINA_HOME and JAVA_HOME 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. According to the settings of other environment variables, JAVA_HOME represents the JDK installation directory, then TOMCAT_HOME should be used to represent the Tomcat installation directory. Why? What about using CATALINA_HOME? In fact, before Tomcat 4, TOMCAT_HOME was used to represent the installation directory of Tomcat. After Tomcat 4, the new servlet container Catalina was used, so the name of the environment variable was also changed to CATALINA_HOME.

The names of environment variables under Windows are case-independent, that is, JAVA_HOME and java_home are the same.

 

了解了startup.bat文件以后,我们再来看看真正负责启动Tomcat服务器的catalina.bat文件。通过分析catalina.bat文件,我们发现它还调用了一个文件setclasspath.bat。在setclasspath.bat文件中,它检查JAVA_HOME环境变量是否存在,并通过JAVA_HOME环境变量,找到java.exe,用于启动Tomcat。在这个文件中,还设置了其他的一些变量,代表调用Java的标准命令,有兴趣的读者可以自行分析一下这个文件。在执行完setclasspath.bat之后,catalina.bat剩下的部分就开始了Tomcat服务器的启动进程。

 

直接执行catalina.bat时,需要带上命令行的参数。读者可以在命令提示符窗口下,执行catalina.bat,就会打印出catalina.bat命令的各种参数及其含义,如下图所示。

technology sharing

(点击查看大图)图     catalina.bat的各参数信息

 

其中常用的参数是start、run和stop。参数start表示在一个单独的窗口中启动Tomcat服务器,参数run表示在当前窗口中启动Tomcat服务器;参数stop表示关闭Tomcat服务器。我们执行startup.bat,实际上执行的就是“catalina.bat start”命令;执行shutdown.bat,实际上执行的是“catalina.bat stop”命令。“catalina.bat run”命令有时候是非常有用的,特别是当我们需要查看Tomcat的出错信息时。

 

When developing JSP programs, it is often encountered that the 8080 port number on your machine is occupied by other applications, or an error occurs when configuring server.xml, when you pass startup.bat (equivalent to executing "catalina.bat start") When starting the Tomcat server, if a serious error occurs during the startup process, since the Tomcat server is started in a separate window, once the startup fails, the command prompt window will be automatically closed, and the error message output during the program operation will disappear. And there is no log information, which makes us no way to find out the cause of the error. When an error occurs, we can replace it with the "catalina.bat run" command to start again. Once the startup fails, only the Tomcat server terminates abnormally, but the error message at startup is still retained in the current command prompt window, so we You can find the reason for the startup failure.

 

 

 

 

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326874749&siteId=291194637