Use the text editing tool to open the batch file startup.bat to start Tomcat 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 determine 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 hierarchy of the Tomcat installation directory on my machine, the value of CATALINA_HOME is set to the Tomcat installation directory. If the environment variable CATALINA_HOME already exists, the "catalina.bat start" command in the bin directory is called through this environment variable. Through this analysis, we learned two information. One is that when Tomcat starts, we need to look up the environment variable CATALINA_HOME. If startup.bat is called in Tomcat's bin directory, Tomcat will automatically and correctly set CATALINA_HOME; the other is to execute the startup.bat command, which is actually executed the "catalina.bat start" command.
If we call startup.bat when the bin directory of Tomcat is the current directory, the error message shown in the figure below will appear (except for calling it in the parent directory of the bin directory).
An error occurred when starting Tomcat in other directories
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 Windows XP system, and its value is the Tomcat installation directory. On my 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 process of adding the JAVA_HOME environment variable mentioned above. If you do not want to add it in the system environment variables, you can also set it directly in the startup.bat file. The following is a 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_HOME=%CURRENT_DIR%"
if exist "%CATALINA_HOME%/bin/catalina.bat" goto okHome
cd ..
...
Note that the purpose of this code displayed in bold is to set the CATALINA_HOME environment variable. Below it is a statement to determine whether CATALINA_HOME is empty. If you can't find the correct location, simply put the code setting 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 in other directories to close the Tomcat server, you need to set the two environment variables, CATALINA_HOME and JAVA_HOME in the shutdown.bat file. The location of the variable is the same as the startup.bat file, both before judging whether CATALINA_HOME is empty. Of course, in order to avoid reinstalling Tomcat once and for all, we need to set it up after reinstalling Tomcat (the same version of Tomcat needs to be installed in the same location), it is best to add the two environment variables, CATALINA_HOME and JAVA_HOME, to the environment variables of 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 installation directory of JDK, so TOMCAT_HOME should be used to represent the installation directory of Tomcat, but why should CATALINA_HOME be used? In fact, before Tomcat 4, TOMCAT_HOME was used to represent the Tomcat installation directory. After Tomcat 4, a new Servlet container Catalina was used, so the name of the environment variable was also changed to CATALINA_HOME.
In Windows system, the name of the environment variable is case-free, that is, JAVA_HOME and java_home are the same.
After understanding the startup.bat file, let’s take a look at the catalina.bat file that is actually responsible for starting the Tomcat server. By analyzing the catalina.bat file, we found that it also calls a file setclasspath.bat. In the setclasspath.bat file, it checks whether the JAVA_HOME environment variable exists, and through the JAVA_HOME environment variable, finds java.exe, which is used to start Tomcat. In this file, some other variables are also set to represent the call to standard Java commands. Interested readers can analyze this file by themselves. After executing setclasspath.bat, the rest of catalina.bat starts the Tomcat server startup process.
When executing catalina.bat directly, you need to bring the command line parameters. Readers can execute catalina.bat in the command prompt window, and they will print out various parameters and their meanings of the catalina.bat command, as shown in the figure below.
(Click to view the large image) Parameter information of catalina.bat in the picture
The commonly used parameters are start, run and stop. The parameter start means starting the Tomcat server in a separate window, the parameter run means starting the Tomcat server in the current window; the parameter stop means closing the Tomcat server. When we execute startup.bat, we actually execute the "catalina.bat start" command; when we execute shutdown.bat, we actually execute the "catalina.bat stop" command. The "catalina.bat run" command is sometimes very useful, especially when we need to view Tomcat's error message.
When developing JSP programs, you often encounter the port number 8080 on your machine being occupied by other applications, or an error occurs when configuring server.xml. When starting the Tomcat server through startup.bat (equivalent to executing "catalina.bat start"), if there is a serious error during startup, since the Tomcat server is started in a separate window, once the startup fails, the command prompt window will automatically close, and the error information output during the program operation will disappear, and there is no log information, which makes it impossible for us to find out the cause of the error. When an error occurs, we can change to the "catalina.bat run" command to start again. Once the startup fails, it is just that the Tomcat server terminates abnormally. However, the error information at startup is still retained in the current command prompt window, so we can find the reason for the startup failure.
The above is to sort out the information for Tomcat's startup analysis, and we will continue to add relevant information in the future. Thank you for your support for this website!