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);