8

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. :'(

Umang Desai
  • 428
  • 2
  • 5
  • 14

1 Answers1

8

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.

Luigi R. Viggiano
  • 8,659
  • 7
  • 53
  • 66
  • Is there a way to set this at runtime/programatically? – Umang Desai Dec 25 '12 at 02:32
  • 2
    pathToAdd is wrong, it should point to the directory, not the dll. You should probably write pathToAdd = "C:/Program Files/Java/jdk1.7.0_03/jre/bin/" (without attach.dll), or BETTER you can do System.load("C:/Program Files/Java/jdk1.7.0_03/jre/bin/attach.dll") isntead – Luigi R. Viggiano Dec 25 '12 at 02:36
  • I tried System.load(path/attach.dll); It doesnt work. I have no idea why. Could it be, because i am using jdk1.7 – Umang Desai Dec 25 '12 at 02:38
  • 2
    have you tried to use "C:/Program Files/Java/jdk1.7.0_03/jre/bin" instead of "C:/Program Files/Java/jdk1.7.0_03/jre/bin/attach.dll" in the pathToAdd?; "Could it be, because i am using jdk1.7" << no, it shouldn't make any difference. – Luigi R. Viggiano Dec 25 '12 at 02:40
  • Thanks man, That worked. i was adding the file instead of adding the path :3. I used this code: http://fahdshariff.blogspot.jp/2011/08/changing-java-library-path-at-runtime.html> This code resets the library path and appends the path i want to add. Cheers – Umang Desai Dec 25 '12 at 02:51
  • ;) you just needed another pair of eyes. I missed it as well at first look. So you can 'accept' my answer, or vote up now :) – Luigi R. Viggiano Dec 25 '12 at 02:59