Home > Blockchain >  What's the alternative for VMSupport?
What's the alternative for VMSupport?

Time:02-26

int java8 the sun.misc.VMSupport util provides multi apis like getAgentProperties() etc. However, this util is removed in java11 release. Is there any alternative for this class?

CodePudding user response:

The Java 11 alternative is the com.sun.tools.attach.VirtualMachine class (javadoc) which provides a getAgentProperties() method.

From the javadoc:

The following example demonstrates how VirtualMachine may be used:

  // attach to target VM
  VirtualMachine vm = VirtualMachine.attach("2177");

  // start management agent
  Properties props = new Properties();
  props.put("com.sun.management.jmxremote.port", "5000");
  vm.startManagementAgent(props);

  // detach
  vm.detach();

You can also get a VirtualMachine object from the AttachProvider API (javadoc).

(Note that the VMSupport class is still in the Java 17 code base as jdk.internal.vm.VMSupport, but Java 17 will stop you using it unless you apply a very big sledgehammer. Just don't.)

  • Related