Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JConsole shows threads when run via ProcessBuilder from the process being monitored (Oracle jdk1.8.0_202_linux-x64)
import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.concurrent.Executors;
public class JConsole8Test {
public static void main(String[] args) throws IOException {
Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, "JConsole8Test")).execute(() -> {
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
});
String javaBin = System.getProperty("java.home") + System.getProperty("file.separator") + "bin";
new ProcessBuilder().directory(new File(javaBin))
.command("jconsole", getProcessId())
.start();
}
// Adapted from:
// https://stackoverflow.com/a/7690178/317760
private static String getProcessId() {
final String jvmName = ManagementFactory.getRuntimeMXBean().getName();
final int index = jvmName.indexOf('@');
if (index < 1) {
// part before '@' empty (index = 0) / '@' not found (index = -1)
return "";
}
try {
return Long.toString(Long.parseLong(jvmName.substring(0, index)));
}
catch (NumberFormatException e) {
return "";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.