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

10,449

Solution 1

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.

Solution 2

I've had luck debugging processes run by the SYSTEM user by running this from a command prompt:

c:> time/t
11:18 AM
c:> at 11:19 /interactive cmd.exe
Added a new job with job ID = 1

Use a time 1 minute in the future. When the "at" job runs a windows command prompt will open running as the user SYSTEM. From there you should be able to see the java processes.

If the service is running as a local user (check the details from "mmc %windir%\system32\SERVICES.MSC" by double clicking on the service and selecting the "Log On" tab) you can do the same thing using "runas":

runas /user:USERNAME cmd.exe

Solution 3

I've only found a suggest to run visuamvm (or other tools) as windows service: Monitoring Java Processes Running As a Windows Service

Perhaps someone else knows a better solution.

Solution 4

You only get those processes that "belong" to you - same user id.

Can you connect to it with jvisualvm?

Solution 5

This is my batch file to record a thread dump:

:: Creates a thread dump for the tomcat6.exe process saved in a timestamped filename and views it!
:: Jim Birch 20111128 rev 2015-10-12

::Note this required the following files to be placed in the confluence jre/bin folder:
:: 
:: attach.dll  - From the Java JDK  (must be the same version)
:: tools.jar   - ditto
:: psexec.exe  - from Windows sysinternals
:: Also, the TEMP directory must be set (stack overflow)
set TEMP=c:\windows\temp

::go to run location
d:
cd \confluence.application\jre\bin

::build datetime filename
rem datetime from wmi.exe
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set dt0=%%I
rem  datetime string as YYYY-MM-DD-hhmmss
set dt=%dt0:~0,4%-%dt0:~4,2%-%dt0:~6,2%-%dt0:~8,6%
set ff=td-%dt%.txt
echo filename: %ff%

::tomcat PID   
FOR /F "tokens=2" %%I in ('TASKLIST /NH /FI "IMAGENAME eq tomcat6.exe"' ) DO SET PID=%%I
ECHO pid: %PID%

::combine above with jstack command (won't work without psexec)
psexec -s "D:\confluence.application\jre\bin\jstack.exe" -l %PID%  >>  %ff%

:: view output txt file
start %ff%

::pause to review script operation, or use ping to wait a few secs 
::ping localhost -n 20 >nul
pause
Share:
10,449

Related videos on Youtube

Matthew McCullough
Author by

Matthew McCullough

Matthew McCullough works for GitHub, Inc. and trains audiences around the world on the most effective use of the Git version control system and GitHub collaboration platform. In supplement to travel-based teaching, he writes books and records videos for O'Reilly and Manning on the topics of delivering technical presentations, the use of modern build and continuous integration tools, and version control industry best practices.

Updated on April 16, 2022

Comments

  • Matthew McCullough
    Matthew McCullough about 2 years

    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?

  • Matthew McCullough
    Matthew McCullough almost 15 years
    Can't get to the processes with jvisualvm either. Apparently jps and jvisualvm use the identical underlying lib/api for their interrogation (jvmstat)
  • ripper234
    ripper234 over 14 years
    See this related question: stackoverflow.com/questions/77528/…
  • ripper234
    ripper234 over 14 years
    I know see the services in jconsole, but can't connect to them (they are grayed out). When I manually try to connect to the port, I can't. CurrPorts shows the correct port is definitely listened to by the JVM.
  • Devon_C_Miller
    Devon_C_Miller over 14 years
    Note: this approach will not work under Vista or Win7 unless you disable the UAC howtogeek.com/howto/windows-vista/…
  • Devon_C_Miller
    Devon_C_Miller over 14 years
    I haven't tried it yet, but here's an article on writing a service that opens a SYSTEM console: codeproject.com/KB/vista-security/SubvertingVistaUAC.aspx
  • MikeOnline
    MikeOnline about 4 years
    The datetime value can be obtained without WMIC, as follows: set DATETIME=%date:~10,4%-%date:~4,2%-%date:~7,2%_%time:~0,2%-%t‌​ime:~3,2%-%time:~6,2‌​% (may need adjustment depending on your Windows Region settings). Also, the tokens value may need to be changed to grab the PID from tasklist output. Finally, the %~dp0 folder where the batch file lives cannot contain any spaces. Run the batch file As Administrator.