1

I am trying to get a unix (solaris and linux) thread dump on a java application.

1) When the java application is a tomcat web application, using kill -3 , the dump goes to the catalina.out file, as this is standard output. kill -3 pid > td.out does not work.

2) For another spring standalone java application, how do I find the standard output for it. I have used: kill -3 pid, and I have checked in my application logs, and I cannot find anything.

Please advise how I can determine standard output for the java application and see the thread dump.

Thanks, B.

user518066
  • 1,277
  • 3
  • 23
  • 35

2 Answers2

4

If you're using OpenJDK or Sun JDK 6 or later, try the jstack command in the bin folder. This is useful when redirecting standard out to a file is problematic for some reason. Execute the following, passing in the Java process ID:

jstack -l JAVA_PID > jstack.out
Amir Afghani
  • 37,814
  • 16
  • 84
  • 124
  • On the unix machine, I don't have access to jdk, only a jre 1.6. Hence I am using kill -3 pid or kill -QUIT pid. – user518066 Nov 06 '13 at 17:55
  • I found this: http://stackoverflow.com/questions/4876274/kill-3-to-get-java-thread-dump?rq=1 Add the following to java program, this will redirect jvm stuff to a file -XX:+UnlockDiagnosticVMOptions -XX:+LogVMOutput -XX:LogFile=jvm.log This works but have to change java arguments. – user518066 Nov 07 '13 at 11:09
0

Try using Process

Process p = null;
String cmd[] = {"bash","-c","ps -C java | awk '{ print $1; }' | sed -n '2{p;q;}'"};
try
{
p = Runtime.getRuntime().exec(cmd);
try
{
p.waitFor();
System.out.println("Executed Successfully");
}catch(Exception e)
 {
e.printStackTrace();
}

}catch(Exception e)
{
e.printStackTrace();
}

Then read the line by BufferReader and use the above function to kill and output the same

Explanation for the command :

ps -C java | awk '{ print $1; }' | sed -n '2{p;q;}'

" ps -C ProcessName " tells the process if its running with pid, here Java is used as ProcessName

" awk '{ print $1; }' " outputs the 1st row of the output

" sed -n '2{p;q;} " to display the 2nd column

sam
  • 120
  • 1
  • 8