I am trying to get a list of all the VMs running on my machine using the Attach API.
This is the code i am using:
import java.lang.reflect.Field;
import java.util.List;
import com.sun.tools.attach.*;
public class JVMListManager
{
static String pathToAdd = "C:/Program Files/Java/jdk1.7.0_03/jre/bin/attach.dll";
public static void setLibraryPath(String path) throws Exception {
System.setProperty( "java.library.path", pathToAdd );
Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
fieldSysPath.setAccessible(true);
fieldSysPath.set(null, null);
}
private void listActiveVM()
{
List<VirtualMachineDescriptor> vm = VirtualMachine.list();
int i= 1;
for(VirtualMachineDescriptor vmD : vm)
{
System.out.println(i + ". " + vmD.displayName());
i++;
}
}
public static void main(String[] args) throws Exception
{
setLibraryPath(pathToAdd);
JVMListManager jvmListManager = new JVMListManager();
jvmListManager.listActiveVM();
}
}
ERROR:
java.util.ServiceConfigurationError: com.sun.tools.attach.spi.AttachProvider: Provider sun.tools.attach.WindowsAttachProvider could not be instantiated: java.lang.UnsatisfiedLinkError: no attach in java.library.path
Please let me know what methods i can use to fix this.
I have also tried using System.load(pathToAdd); Also i have referred to this Blog post, but it does not work. :'(
You need to:
set PATH=%PATH%;C:/Program Files/Java/jdk1.7.0_03/jre/bin/ (on Windows)
export LD_LIBRARY_PATH=path/to/your/library/dir/ (on Linux or OSX)
to comply to the path of your native library, before starting the jvm.
I don't think that System.setProperty( "java.library.path", pathToAdd );
is working; and this is probably the cause of the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With