Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fix the: java.lang.UnsatisfiedLinkError: no attach in java.library.path

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

like image 256
Umang Desai Avatar asked Dec 25 '12 02:12

Umang Desai


1 Answers

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.

like image 127
Luigi R. Viggiano Avatar answered Oct 31 '22 22:10

Luigi R. Viggiano