It's often convenient to run jcmd command programmatically from within a testcase to get some information or to adjust options.
See the template below:
// import javax.management.ObjectName;
// import javax.management.MBeanServer;
public static void reloadCompilerDirectives(String filename) {
String result;
result = doRunDcmd("compilerDirectivesClear", new String[]{""});
result = doRunDcmd("compilerDirectivesAdd", new String[]{ filename });
}
private static String doRunDcmd(String operationName, String operationArgs[]) {
ObjectName objectName =
new ObjectName("com.sun.management:type=DiagnosticCommand");
MBeanServer mbeanServer =
ManagementFactory.getPlatformMBeanServer();
Object[] params = new Object[] { operationArgs };
String[] signature = new String[]{ String[].class.getName() };
return(String) mbeanServer.invoke(objectName, operationName, params, signature);
}