I tried to debug Tomcat with jvisualvm today, but found that Tomcat could not be found in jvisualvm. It turned out that the problem was caused by the temporary directory setting of Tomcat. Jvisualvm uses PID to find the local Java application. The PID file is stored in the temporary directory.Username> Folder. The temporary directory of Tomcat uses catalina_tmpdir:
%_EXECJAVA% %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%
The catalina_tmpdir directory is the Tomcat directory by default.
if not "%CATALINA_TMPDIR%" == "" goto gotTmpdirset "CATALINA_TMPDIR=%CATALINA_BASE%\temp":gotTmpdir
The temporary directory of jvisualvm uses the temporary directory of the operating system by default. Therefore, jvsualvm cannot read the PID file of Tomcat and thus cannot debug tomcat.
The solution is to make jvsualvm and tomcat use the same temporary directory and set the catalina_tmpdir directory of Tomcat to the temporary directory of the system. You can add environment variables to the operating system, you can also add it in the Tomcat script.