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?
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
In JDK 9 or later, you can get the pid from ProcessHandle;
In earlier releases, you can get the pid from RuntimeMXBean:
Another alternative is to do it over JMX using DiagnosticCommandMXBean.