Problem scenario
You run Java programs on your Linux server. You want to view content of Java thread dumps. What should you do?
Solution
Prerequisites
Install Java if it has not already been installed. See this posting if you need assistance.
You may or may not want to learn more about what a Java thread is; you can read more here.
Procedures
- Find the PID of the Java program. To do this, run this:
sudo ps -ef | grep java
You should see this:
ubuntu 13687 13579 0 20:45 pts/3 00:00:00 java ContIntSleep
ubuntu 13702 13499 0 20:45 pts/2 00:00:00 grep --color=auto java
For the example above, 13687 is the PID you need. If you want a Java program that uses the sleep command (so it lasts for a while) use this program:
class ContIntSleep extends Thread{
public void run(){
for(int i=1;i<50;i++){
try{Thread.sleep(500);}catch(InterruptedException e){System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
ContIntSleep contint1=new ContIntSleep();
ContIntSleep contint2=new ContIntSleep();
contint1.start();
contint2.start();
}
}
Run this (but replace 13687 with the PID you found above):
jstack 13687
If you want to redirect the output to a file, you could run this:
jstack 13687 > /tmp/goodfile.txt