X

An Oracle blog about NetBeans Profiler

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 setupcons: 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 servicescons: 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 servicescons: 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.

Join the discussion

Comments ( 11 )
  • rzara Thursday, January 22, 2009

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


  • Geertjan Thursday, January 22, 2009

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


  • brian harris Saturday, January 24, 2009

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


  • Loki Monday, August 17, 2009

    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


  • Johannes Hjorteen Tuesday, September 8, 2009

    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?


  • Krishna R Saturday, September 26, 2009

    @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.


  • Karl Peterbauer Wednesday, September 30, 2009

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


  • Jiri Sedlacek Wednesday, September 30, 2009

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


  • hmhm Tuesday, January 26, 2010

    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.


  • guest Wednesday, May 11, 2011
    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.
  • dberkley Monday, July 25, 2011

    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.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.