Thursday, March 21, 2013

Monitoring Windows Tomcat Service with Java VisualVM
(and how I finally failed)

Local Java Applications that are running as a Windows Service can not be monitored with Java VisualVM without some extra efforts. I recently tried to monitor a Tomcat service but I could not find it after starting Java VisualVM. I wondered what was wrong and found the best answer in an old Netbeans Profiler Blog post: "Monitoring Java Processes Running As a Windows Service". The reason why I did not see the Tomcat Service in the application tree was:

By default, when you start VisualVM only Java applications started by the same user are listed in the Applications tree. 

The post recommends to use one of the following solutions if you need to monitor a Java Windows Service:
  • use a JMX connection
  • run VisualVM as a service
  • run jstatd as a service
There are some pros and cons for each solution (mentioned in the post). Some solutions seem to be quite complicated and others are possibly not supported on current windows versions.

So if the problem is the user that runs the applications why not make him the same? You can either let the Tomcat service run with your normal user account or...

Use the System Account to start Java Visual VM

Windows Services most likely are started using the Windows System Account. To monitor these services we need to start Java VisualVM with the system account. This can easily be done by using the PsExec tool of the Sysinternals Suite. So start a Windows command line with administrator privileges and type

C:\<...>\SysinternalsSuite>psexec -i -s cmd.exe

Use the new command window to start Java Visual VM:

C:\Program Files\Java\jdk1.7.0_17\bin>jvisualvm.exe

And Java VisualVM opens including the Tomcat process:

But stop! The process is not named "Tomcat" as expected and no monitoring options are available. Instead the hint "Not supported for this JVM." is shown.

Also jps only prints

C:\Program Files\Java\jdk1.7.0_17\bin>jps
4348 Jps
1968 -- process information unavailable

This is the point where I finally failed! I tried hard to find a solution and
  • deinstalled all Java and Tomcat versions and did a complete new installation of
    • jdk-7u17-windows-x64.exe
    • apache-tomcat-7.0.37.exe
  • deleted all temp directories named like "hsperfdata_*" like suggested here
but nothing worked. I am not able to monitor the shown process.

So if you have any solution to get around this behaviour please let me (and others) know.  I would love to see something like this (achieved by running Tomcat and Java VisualVM from the command line):

By the way - the possibly working JMX solution is not sufficient in this case because I would like to have the complete Java VisualVM capabilities (including profiling).

My system coordinates:
  • Windows 7 - 64 bit
  •  java version "1.7.0_17"
    Java(TM) SE Runtime Environment (build 1.7.0_17-b02)
    Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)
  •  Apache Tomcat 7.0.37