[英]How to give command line option in java program?
I want to use Java flight recorder while running my java program.我想在运行我的 Java 程序时使用 Java 飞行记录器。 Can I pass arguments in java program itself and not while running the application.
我可以在java程序本身而不是在运行应用程序时传递参数吗?
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 类说“HelloWorld.class”我想在运行它时使用 java 飞行记录器,我可以用它来实现
java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder HelloWorld
java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder HelloWorld
But I want to achieve this unlocking commercial feature and start flight recorder from my java code.但是我想实现这个解锁商业功能并从我的java代码启动飞行记录器。 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.只有在您拥有 Oracle 的商业许可时才应该这样做,并且不应将其放入库代码中,以免其他人在他们不知情的情况下错误地在生产中解锁它。
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;在 JDK 9 或更高版本中,您可以从 ProcessHandle 中获取 pid;
ProcessHandle.current().pid()
In earlier releases, you can get the pid from RuntimeMXBean:在早期版本中,您可以从 RuntimeMXBean 获取 pid:
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.另一种选择是使用 DiagnosticCommandMXBean 通过 JMX 来完成。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.