Cannot debug windows java services using jhat, jps, jstack

I often show jhat, jps , and the jstack tool is for Linux and Mac developers. However, a developer recently indicated that they are not suitable for use on Windows if the Java application in question is running as a Windows service .

A Fixed a bug with the Sun bug, but it was closed due to inaction.

I checked it for myself, and it really seems true, although I can hardly believe it. Here is the setup:

  • Tomcat or similar launch as a Windows service with the "Logon" == "Local system"
  • A user with administrator rights registered on the same Windows machine.
  • The administrator opens the Windows Task Manager, can see how java.exe works
  • The administrator opens the console, dials "jps", returns a list of processes that do not include the Tomcat java service process.
  • Like brute force attempt to get tomcat PID as a service from windows task manager. Type jstack <pid>. Get the answer: <pid> there is no such process

This looks reproducible in Windows XP, Windows 2003 Server and Windows 7. Java versions 1.5 and 1.6 give the same result.

Is there a way from the terminal, even if it logs in as Admin, for "sudo up" to get JPS and other tools for viewing the java service?

+13
java windows windows-services
Jul 29 '09 at 3:27
source share
6 answers

To run the utility, you can connect to the console session using "mstsc / admin" using the account (not necessarily the exact permissions that it must have, mine was in the Administrators group) and use the sysinternals psexec tool to run in system quality. The following is an example of using jstack.exe:

psexec -s "%JAVA_HOME%\bin\jstack.exe" PID >stack.txt 
Where PID is the process identifier of your process. You may also need to substitute the real path to your JDK depending on your specific environment.

In addition, the TEMP directory must be installed correctly or again the tools will not work.

+8
Apr 05 '10 at 14:50
source share

I had remote debugging processes performed by the SYSTEM user by running this from the command line:

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

Use a time of 1 minute in the future. When the at task runs the Windows command prompt, it will run as a user system. From there you can see java processes.

If the service is started as a local user (check the data with "mmc% windir% \ system32 \ SERVICES.MSC" by double-clicking on this service and selecting the "Login" tab), you can do the same using "runas":

 runas /user:USERNAME cmd.exe 
+4
Jul 31 '09 at 15:26
source share

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

Can you connect to it using jvisualvm?

+1
Jul 29 '09 at 9:02
source share

I just found a suggestion to run visuamvm (or other tools) as a Windows service: Monitoring Java processes running as a Windows service

Maybe someone else knows a better solution.

+1
Sep 07 '09 at 7:13
source share

This is my batch file to write a stream 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 
+1
Dec 17 '15 at 5:14
source share

Here is a solution that ultimately helped me get stack traces from an unprepared Java process running as a different user in a terminal session or as a Windows service.

jstack.cmd :

 @for /f "usebackq tokens=2" %%I in ('tasklist /NH /FI "imagename eq tomcat.exe" /FI "username eq SYSTEM"') DO set PID=%%I @for /f "tokens=2 delims==." %%G in ('wmic os get localdatetime /value') do @set datetime=%%G @set date_time=%datetime:~0,4%_%datetime:~4,2%_%datetime:~6,2%__%datetime:~8,2%_%datetime:~10,2%_%datetime:~12,2% PsExec.exe /accepteula -h -s -d cmd /c "%JAVA_HOME%\jstack.exe" -l %PID% ^>%~dp0jstack_%date_time%.txt 

The first line identifies the PID by process name and user.

Then PsExec with adopted EULA helps to call jstack in elevated mode with properly escaped output redirection in the jstack*.txt dir directory with the time stamp included in the file name .

You may need to add the PsExec and JDK directories to your PATH in advance.

Earlier, I tried to force a thread stream using SendSignal, but it never worked reliably with Windows service processes.

0
Dec 08 '17 at 23:02 on
source share



All Articles