我试图使用Attach API获取在我的机器上运行的所有VM的列表。修复:java.lang.UnsatisfiedLinkError:在java.library.path中不附加
这是我使用的代码:
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();
}
}
错误:
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
请让我知道我可以用什么方法来解决这个问题。
我也试过使用System.load(pathToAdd); 我也提到这个Blog post,但它不起作用。 :'(
请参阅http://stackoverflow.com/questions/5565356/java-attach-api-changing-java-library-path-dynamically –
你的pathToAdd直接指向dll,它应该指向包含dll的文件夹。 –