Monitoring Java Processes Running As a Windows Service

This post describes how Java processes running as a Windows service can be monitored and/or profiled using VisualVM. By default, when you start VisualVM only Java applications started by the same user are listed in the Applications tree. But sometimes you may need to monitor or profile for example Tomcat running as a Windows service. There are several ways to do it using VisualVM:

Using JMX connection

pros: easy to setup
cons: profiling not available

JMX connection can be used to monitor any local/remote Java application in VisualVM incl. the locally or remotely running Java services. All you need to do is to start the service to be monitored with some extra arguments defining JMX connection parameters. Details on how to setup a JMX-enabled application are available here. Once started, you can connect to the application using the Add JMX Connection action. Drawback of this solution is that you cannot profile applications defined by JMX connection, only the monitoring features are available.

Running VisualVM as a service

pros: allows profiling services
cons: not available on Windows Vista

Since only Java processes running under the same user as VisualVM can be profiled, the only way to profile Windows service (which is by default running under the System account) is to start the VisualVM itself as a Windows service. Note that this approach doesn't work on Windows Vista due to security restrictions which by default prevent the services to display any UI. If you have appropriate permissions (ideally perform as Administrator) follow these steps:

  1. Get the instsrv.exe and srvany.exe tools if you don't have them already, they are for example part of the Windows Server 2003 Resource Kit Tools.
  2. Create a Windows service to launch the VisualVM using this command:
       c:\\wrk\\instsrv.exe VisualVM c:\\wrk\\srvany.exe
    This will create a new srvany service (a wrapper service to start any executable) called VisualVM. General instructions on creating new Windows services are available here.



  3. Using a registry editor (regedit.exe, regedt32.exe) create a new Key called Parameters in HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\VisualVM. Then inside the Parameters key create a new String value (REG_SZ) called Application containing a full path to visualvm.exe. This will instruct the srvany service to start VisualVM.



  4. Make sure the service is configured to run under Local System account with 'Allow service to interact with desktop' option enabled. Also, it's a good idea to set the service Start up type to Manual.



Once you have performed the steps, you can simply start the VisualVM service which will launch the tool in context of System account. After confirming the VisualVM license dialog you should see all the running Java services in Applications tree and all the features incl. profiling should be available.

Running jstatd as a service

pros: automatic discovery of remote services
cons: profiling not available, works only remotely

Instead of running the VisualVM itself as a service, you can run the jstatd daemon as a service. This will make all running Java services visible to the jvmstat technology used by VisualVM to automatically discover Java applications. Note that you cannot use this approach for monitoring locally running Java services, it only works remotely. In general, the steps are exactly the same as for running VisualVM itself as a service:

  1. Get the instsrv.exe and srvany.exe tools if you don't have them already, they are for example part of the Windows Server 2003 Resource Kit Tools.
  2. Create a Windows service to launch the jstatd.exe using this command:
       c:\\wrk\\instsrv.exe jstatd c:\\wrk\\srvany.exe
    This will create a new srvany service (a wrapper service to start any executable) called jstatd. General instructions on creating new Windows services are available here.



  3. Using a registry editor (regedit.exe, regedt32.exe) create a new Key called Parameters in HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\jstatd. Then inside the Parameters key create a new String value (REG_SZ) called Application containing a full path to jstatd.exe and security parameters. This will instruct the srvany service to start the jstatd daemon. See the jstatd man page for details on setting up the daemon.



  4. Make sure the service is configured to run under Local System account. Note that the 'Allow service to interact with desktop' option doesn't need to be enabled as jstatd.exe is a command-line tool without any UI. Also, it's a good idea to set the service Start up type to Manual.



Once you have performed the steps, you can simply start the jstatd service and the host as a new host in the remote VisualVM. All the Java services should then be displayed under the host node in Applications tree. Details on monitoring remote applications are available here. Note that profiling won't be available for these applications.

Comments:

You can also launch a cmd.exe as Local System (see http://blogs.msdn.com/adioltean/articles/271063.aspx) and launch visualvm from that

Posted by rzara on January 22, 2009 at 11:49 AM CET #

Why not repost this on http://java.dzone.com? Many more people would read it.

Posted by Geertjan on January 22, 2009 at 03:02 PM CET #

You can also remote desktop to the console session and run it there.

Posted by brian harris on January 24, 2009 at 10:32 PM CET #

I made all steps of "Running VisualVM as a service", but then i start the service i can not see the VisualVM window. I see the VisualVM throw in windows task manager.
OS is Windows 2003 Server

Posted by Loki on August 17, 2009 at 06:35 AM CEST #

I did as you said about jstatd service but when I'm trying to start the service it stops right away. When I use the commandline in cmd.exe it works fine. Any ideas why it stops?

Posted by Johannes Hjorteen on September 08, 2009 at 07:02 AM CEST #

@Loki,,

Is this Win2003 machine, a local one or have you logged in remotely.

The desktop interaction works only with Console sessions, so if it is remote, you may need to try with "mstsc /console -v hostname" and then try the same.

Posted by Krishna R on September 27, 2009 at 01:11 AM CEST #

The whole JMX stuff is fundamentally broken when it comes to Windows services. Sad, but Sun just doesn't care.

Posted by Karl Peterbauer on September 30, 2009 at 08:19 AM CEST #

To Karl: Do you have any concrete problem with JMX connections on Windows services? Everything seems to work just fine for me.

Posted by Jiri Sedlacek on September 30, 2009 at 08:31 AM CEST #

i can run jconsole as service in windows 7
but it doesn't show the running java
processes. and windows 7 sais:

WARNING: The service jconsolesvc is configured as interactive whose support is
being deprecated. The service may not function properly.

Posted by hmhm on January 26, 2010 at 03:33 AM CET #

To run jstatd as a service on windows 2008 R2 64bit install XYNTService (http://www.codeproject.com/KB/system/xyntservice.aspx) as windows resource kit is not supported for 2008. It is a service wrapping other application that do not support service protocol like start and stop.

Posted by guest on May 11, 2011 at 10:02 AM CEST #

If you are running remotely using Remote Desktop, the UI will not pop up. Add " /admin" to the server name when you sign in to get around that problem.

Posted by dberkley on July 25, 2011 at 09:04 AM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

A blog by NetBeans Profiler & VisualVM developers.

Search

Archives
« May 2015
SunMonTueWedThuFriSat
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
      
Today