Saad Ousman
Saad Ousman

Reputation: 17

How to Obtain Java Thread Dump in JRE 1.8.0_144

I've been trying to find ways to obtain the Thread Dump from a Java Application on my Windows server running on jre 1.8.0_144.

Non of the monitoring utilities like jcmd jstack jconsole are available in either the bin or lib folders of the Java environment directory.

I have come across several applications online that claim to perform the same task but haven't found a reliable one yet.

Changing the JRE version, unfortunately, has been ruled out as an option

Upvotes: 1

Views: 799

Answers (1)

swpalmer
swpalmer

Reputation: 4380

There is a way if you are running with tools.jar available, that is running from a JDK instead of a stock JRE. However given that you don't have the jcmd, jstack, jconsole tools available, this is unlikely.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;

// You need to add tools.jar to the classpath to get this
import com.sun.tools.attach.VirtualMachine;

public class Main {

    public static void main(String[] args) throws Exception {
        // Here you will need to use JNI or JNA to get the current process' PID
        // because the JRE version you are using doesn't have any other way.
        long pid = getCurrentPid(); // you need to create this method
        VirtualMachine vm = VirtualMachine.attach(""+pid);
            Method m = vm.getClass().getMethod("remoteDataDump", Object[].class);
            InputStream in = (InputStream) m.invoke(vm, new Object[] { new Object[0] } ); // awkward due to nested var-args
            try (BufferedReader buffer = new BufferedReader(new InputStreamReader(in))) {
                buffer.lines().forEach(System.out::println);
            }
    }
}

Upvotes: 0

Related Questions