5

Two questions I am having are:

  1. How to Import lib for jmx(i can't import it)?

  2. Can we access Java Mission Control using Code? (like I can see the visualisation of my problem but I want to fetch it into my IDE using code), is it possible?

Coder
  • 1,129
  • 10
  • 24
  • Possible duplicate of [How to add enable flag for Flight Recorder in Maven project?](https://stackoverflow.com/questions/25518834/how-to-add-enable-flag-for-flight-recorder-in-maven-project) – JoSSte Nov 19 '18 at 13:22
  • 1
    Which IDE are you using? which sort of project are you using? If you are using maven, see the duplicate question https://stackoverflow.com/questions/25518834/how-to-add-enable-flag-for-flight-recorder-in-maven-project otherwise, download the .jar file and put in the classpath/lib folder of your project – JoSSte Nov 19 '18 at 13:23
  • To use Java Mission Control libraries in your code, this might be useful: http://hirt.se/blog/?p=920 – Klara Nov 19 '18 at 16:24
  • if there is any .jar file for it , can anyone give me a link here? i can't find it – Coder Nov 20 '18 at 09:40

1 Answers1

5

If you are using Oracle JDK 9+ or OpenJDK 11+, you can access the data in a JFR file using the Flight Recorder API.

For example, to print all the events:

import jdk.jfr.consumer.*;

try (RecordingFile r = new RecordingFile(Path.of("recording.jfr"))) {
 while (r.hasMoreEvents()) {
   System.out.println(r.readEvent());
 }
}

For more information about the API: https://docs.oracle.com/en/java/javase/11/docs/api/jdk.jfr/jdk/jfr/consumer/package-summary.html

Kire Haglin
  • 6,569
  • 22
  • 27
  • can we do the same thing without using Jfr and Jmc? – Coder Nov 21 '18 at 11:55
  • If you want to access Flight recorder data, you need a parser that can read the files.There are two parsers, one that comes with JDK (jdk.jfr.consumer.*) and one that ships with JMC. You also changed your question to be about JMX. which there is also an API for in the JDK (java.management.*) – Kire Haglin Nov 22 '18 at 05:20
  • no need to change the question , that was exactly what i was looking for, this is just curiosity Question . – Coder Nov 22 '18 at 06:49