2

I want to use Java flight recorder while running my java program. Can I pass arguments in java program itself and not while running the application.

For ex: I have a java class say "HelloWorld.class" I want to use java flight recorder while running this which i can achieve with

java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder HelloWorld

But I want to achieve this unlocking commercial feature and start flight recorder from my java code. Is it possible?

schaturv
  • 122
  • 8

1 Answers1

2

You should only do this if you have a commercial license from Oracle, and you should not put it into library code, so others by mistake unlocks it in production without their knowledge.

At runtime, it's probably easiest to do

Runtime.getRuntime().exec("jcmd " + pid + " VM.unlock_commercial_features");

In JDK 9 or later, you can get the pid from ProcessHandle;

ProcessHandle.current().pid() 

In earlier releases, you can get the pid from RuntimeMXBean:

String jvmName = ManagementFactory.getRuntimeMXBean().getName();
int index = jvmName.indexOf("@");
String pid = jvmName.substring(0, index);

Another alternative is to do it over JMX using DiagnosticCommandMXBean.

    ObjectName on = new ObjectName("com.sun.management:type=DiagnosticCommand"); 
    Object[] a = new Object[] {
        new String[] {
        }
    };
    String[] sig = new String[] {"[Ljava.lang.String;"};
    ManagementFactory.getPlatformMBeanServer().invoke(on, "vmUnlockCommercialFeatures", a, sig);
Kire Haglin
  • 6,569
  • 22
  • 27
  • Thanks for your response @Kire I tried this long pid = ProcessHandle.current().pid(); Runtime.getRuntime().exec("jcmd " + pid + " VM.unlock_commercial_features"); Runtime.getRuntime().exec("jcmd " + pid + " JFR.start"); But its still complaining about unlocking commercial feature while running the program. I am using JDK 9. – schaturv Apr 29 '19 at 07:17
  • I can't reproduce with JDK 9.0.1. Did "Runtime.getRuntime().exec("jcmd " + ProcessHandle.current().pid() + " VM.unlock_commercial_features");" – Kire Haglin Apr 29 '19 at 12:43
  • I have used 9.0.4 and these lines Runtime.getRuntime().exec("jcmd " + ProcessHandle.current().pid() + " VM.unlock_commercial_features"); Runtime.getRuntime().exec("jcmd " + ProcessHandle.current().pid() + " JFR.start"); Its not giving any error on these lines but when I am trying to use jfr for some Recording its saying, it should be unlocked first. – schaturv Apr 30 '19 at 06:56
  • Hod did you start the recording programmatically? I tried: Recording r = new Recording(Configuration.getConfiguration("default")); r.start() – Kire Haglin Apr 30 '19 at 07:00
  • I did like this – schaturv Apr 30 '19 at 07:31
  • try (Recording recording = new Recording()) { recording.setName("MY Recording"); recording.start(); – schaturv Apr 30 '19 at 07:31
  • I tried this as well try (Recording recording = new Recording(Configuration.getConfiguration("default"))) { recording.setName("MY Recording"); recording.start(); – schaturv Apr 30 '19 at 07:39
  • Are you starting the program so it can run jcmd in the shell (i.e. JDK_HOME/bin) – Kire Haglin Apr 30 '19 at 14:44
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/192639/discussion-between-kire-haglin-and-schaturv). – Kire Haglin Apr 30 '19 at 14:49