1 Creating and Recording Your First Event

The sample HelloWorldSample.java creates an event named com.oracle.Hello.

import jdk.jfr.Event;
import jdk.jfr.Label;
import jdk.jfr.Name;

public class HelloWorldSample {

    @Name("com.oracle.Hello")
    @Label("Hello World!")
    static class Hello extends Event {
        @Label("Message")
        String message;
    }

    public static void main(String... args) {
        Hello event = new Hello();
        event.begin();
        event.message = "Hello world!";
        event.commit();
    }
}

Run HelloWorldSample with the following command:

java -XX:StartFlightRecording:filename=hw.jfr HelloWorldSample.java

It runs HelloWorldSample and creates a recording file named hw.jfr.

To view the contents of the recording file, run this command:

jfr print hw.jfr

It prints all events recorded by Flight Recorder.

If you only want to view the Hello event that you created, then run this command:

jfr print --events Hello hw.jfr

It prints output similar to the following:

com.oracle.Hello {
  startTime = 16:44:14.841
  duration = 0.0170 ms
  message = "Hello world!"
  eventThread = "main" (javaThreadId = 1)
  stackTrace = [
    HelloWorldSample.main(String[]) line: 18
    jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Method, Object, Object[])
    jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Object, Object[]) line: 64
    jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43
    java.lang.reflect.Method.invoke(Object, Object[]) line: 564
    ...
  ]
}