Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't debug Java Windows Services with jhat, jps, jstack

I frequently showcase the jhat, jps, and jstack tool set to developers on Linux and Mac. However, a developer recently indicated that these are unusable in Windows if the Java app in question is running as a Windows Service.

A Sun-filed bug says something very similar, but was closed due to inactivity.

I have tested this out for myself, and indeed it appears true, though I can hardly believe it. Here is the setup:

  1. Tomcat or similar running as a Windows service with the "Log On As" == "Local System"
  2. A user with Admin privileges logged in to the same Windows machine.
  3. Admin opens Windows Task Manager, can see java.exe running
  4. Admin opens console, types "jps", gets back a list of processes that does not include Tomcat's java service process.
  5. As a brute force attempt, get the PID of tomcat as a service from Windows Task Manager. Type jstack < pid >. Get a reply: < pid > no such process

This appears reproducible under Windows XP, Windows 2003 Server, and Windows 7. Java versions 1.5 and 1.6 yield the same outcome.

Is there a way from the terminal, even though logged in as Admin, to "sudo up" to get JPS and the other tools to see the java service?

like image 512
Matthew McCullough Avatar asked Jul 29 '09 03:07

Matthew McCullough


1 Answers

To get the utilities to run, you can connect to the Console session using "mstsc /admin" using a login account (not sure exact permissions it needs to have, mine was in the Administrators group) and use the Sysinternals psexec tool to run as system. Here is an example of using jstack.exe:

psexec -s "%JAVA_HOME%\bin\jstack.exe" PID >stack.txt
Where PID is the process ID of your process. You may also have to substitute the actual path to your JDK depending upon your specific environment.

Also, the TEMP directory must be set correctly or once again the tools will not work.

like image 163
Rob Tanzola Avatar answered Sep 26 '22 03:09

Rob Tanzola