转自:
http://blog.csdn.net/gold_fly/article/details/7732413
http://techiedan.com/2009/10/19/set-up-jdk-in-eclipse/
偶然一天,运行eclipse单元测试,报错:
- java.lang.IllegalStateException: Native library for Attach API not available in this JRE
- at mockit.internal.startup.JDK6AgentLoader.getVirtualMachineImplementationFromEmbeddedOnes(JDK6AgentLoader.java:81)
- at mockit.internal.startup.JDK6AgentLoader.loadAgent(JDK6AgentLoader.java:54)
- at mockit.internal.startup.AgentInitialization.initializeAccordingToJDKVersion(AgentInitialization.java:21)
- at mockit.internal.startup.Startup.initializeIfNeeded(Startup.java:208)
- java.lang.IllegalStateException: Native library for Attach API not available in this JRE
- at mockit.internal.startup.JDK6AgentLoader.getVirtualMachineImplementationFromEmbeddedOnes(JDK6AgentLoader.java:81)
- at mockit.internal.startup.JDK6AgentLoader.loadAgent(JDK6AgentLoader.java:54)
- at mockit.internal.startup.AgentInitialization.initializeAccordingToJDKVersion(AgentInitialization.java:21)
- at mockit.internal.startup.Startup.initializeIfNeeded(Startup.java:208)
检查了下,发现由于最近jmockit升级,导致eclipse的参数出了问题影响的。进入eclipse-->preference-->TestNG-->Run/Debug,这里有个设置运行时JVM args,把jmockit.jar设置下,格式为:-javaagent:D:\repository\com\alibaba\external\test.jmockit\0.999.11\test.jmockit-0.999.11.jar。
重新跑单元测试,运行正常!问题解决;
这个出错有点奇怪,通过出错类,进行跟踪,发现最后的出错信息如下:
- Class JDK6AgentLoader{
- private VirtualMachine attachToThisVM()
- {
- try {
- return VirtualMachine.attach(pid);
- }
- catch (AttachNotSupportedException e) {
- throw new RuntimeException(e);
- }
- catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- private VirtualMachine getVirtualMachineImplementationFromEmbeddedOnes()
- {
- try {
- if (File.separatorChar == '\\') {
- return new WindowsVirtualMachine(ATTACH_PROVIDER, pid);
- }
- else {
- return new LinuxVirtualMachine(ATTACH_PROVIDER, pid);
- }
- }
- catch (AttachNotSupportedException e) {
- throw new RuntimeException(e);
- }
- catch (IOException e) {
- throw new RuntimeException(e);
- }
- catch (UnsatisfiedLinkError e) {
- throw new IllegalStateException("Native library for Attach API not available in this JRE", e);
- }
- }
通过文件路径判断是windwo,还是linux操作系统,从代码看catch并抛出异常的处理很常见;
- WindowsVirtualMachine{
- static
- /* */ {
- /* 167 */ System.loadLibrary("attach");
- /* 168 */ init();
- /* 169 */ stub = generateStub();
- /* */ }
- }
jmockit里面用到了jdk6.0的不少特性,jdk6.0的动态修改和装载还是挺强大的。
【yasi】遇到相同的问题,是这样解决的:
参考这篇帖子,将工程的Library中的 JRE7 换成 JDK 1.7,如图