The pit of CATALINA_HOME environment variable

Problem Description

Today's task is to add an interface to a Web service for another Web service to access through Http.
The IDE used is IDEA. I haven't figured out how to start two web services in IDEA for debugging at the same time (and these two web services belong to two modules of the same project). Therefore, the method adopted is to separately adjust the post and joint debugging. The steps are as follows:
-Start the service A that provides the interface in the IDE, directly access the corresponding URL through the browser, and debug the interface of the A service;
-Publish in the local Tomcat Service A, and start Tomcat;
-Start service B calling the interface in the IDE, and access the interface of Service A in Tomcat.


It seems that there should be no problem. However, strange things have appeared:
-Service B prompts 404 NOT FOUND when it is called;-Accessing
the interface of service A directly in the browser is also 404 NOT FOUND;
-Restarting service A in IDEA , Access interface, no problem;
-repost it again, still 404;-accessing
an old interface of service A in the browser is normal...

identify the problem

It's impossible to hell, so there must be something wrong.
From the perspective of the problem, the new interface 404 and the old interface 200. It looks like the new interface is missing!
When starting in IDEA, the service port is Tomcat's default port 8080. When starting in a standalone Tomcat, the port is changed to 8088.
Since the new interface is not found at all, there is no log to locate the problem.
Facing the command line window of Tomcat for a long time, I suddenly found out: Why is the port 8099 started by this Tomcat? ? ?
8099 is the access port provided for the test before, and the inbound and outbound rules are configured.
But I obviously changed the port to 8088!

Here is an explanation: Due to project switching, there are a total of four or five copies of Tomcat on this machine; two of them are version 9.0 and three are version 8.0 (there is a problem with the outsourcing control using version 9.0). One Tomcat9.0 is used for IDEA embedded use, and the other Tomcat9.0 provides test access. The remaining three 8.0s are newly added, and the new services are configured in these Tomcats of 8.0.
Write picture description here

At this time, I understand a little bit. Although the startup command of Tomcat calls startup.bat in 8.0, it actually starts startup.bat in 9.0. It just so happens that service A is also deployed in this 9.0, but it is an old version of service A (there is an old interface, and the new interface is definitely not there).
As for why the Tomcat in 9.0 is started from the bat batch in 8.0, it is necessary to find the reason from the environment variables.
When Tomcat was first installed, in order to facilitate startup, the environment variable CATALINA_HOME was configured, and it was this environment variable that caused this coincidental bug.

Ask the bottom line

   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 ..
    set "CATALINA_HOME=%cd%"
    cd "%CURRENT_DIR%"
    :gotHome
    if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
    echo The CATALINA_HOME environment variable is not defined correctly
    echo This environment variable is needed to run this program
    goto end
    :okHome

    set "EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat"

    rem Check that target executable exists
    if exist "%EXECUTABLE%" goto okExec
    echo Cannot find "%EXECUTABLE%"
    echo This file is needed to run this program
    goto end
    :okExec

    rem Get remaining unshifted command line arguments and save them in the
    set CMD_LINE_ARGS=
    :setArgs
    if ""%1""=="""" goto doneSetArgs
    set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
    shift
    goto setArgs
    :doneSetArgs

    call "%EXECUTABLE%" start %CMD_LINE_ARGS%

    :end

The above is a piece of code in the startup.bat batch command, roughly translated:
1. If the CATALINA_HOME environment variable is not defined, set the CATALINA_HOM environment variable to the startup directory of the bat file (here is the current directory);
2. If CATALINA_HOME is not If it is empty, judge whether there is %CATALINA_HOME%\bin\catalina.bat (that is, find another batch file in the bin directory, catalina.bat); otherwise, set CATALINA_HOME to the current directory and continue to find catalina.bat;
3. Execute catalina.bat to start tomcat;
so you can understand that if the environment variable CATALINA_HOME is set, no matter from which tomat startup.bat is started, catalina.bat in the directory corresponding to the environment variable will be found to start the corresponding tomcat.

Solution

The solution is: remove the environment variable CATALINA_HOME and let each Tomcat start directly from the directory where startup.bat is located.

Looking back at the emergence of the whole problem, I have to say something that is a bit coincidental. However, it has to be said that when Tomcat was initially configured, it was mechanically followed the steps on the Internet, and I did not understand the meaning behind it. Take time to study catalina.bat to see what this batch has done.

 

Original source: https://blog.csdn.net/achang07/article/details/77765533

Guess you like

Origin blog.csdn.net/dgxin_605/article/details/81091228