24

I was trying to use jmockit to unit test my project and got the following error:

java.lang.UnsatisfiedLinkError: no attach in java.library.path
java.lang.IllegalStateException: Native library for Attach API not available in this JRE
    at mockit.internal.startup.JDK6AgentLoader.getVirtualMachineImplementationFromEmbeddedOnes(JDK6AgentLoader.java:95)
    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:98)
    at mockit.internal.startup.Startup.initializeIfPossible(Startup.java:112)
    at org.junit.runner.Runner.<clinit>(Runner.java:22)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.UnsatisfiedLinkError: no attach in java.library.path

I have included jdk6/lib/tools.jar, jmockit.jar, and junit.jar in the classpath, respectively. Any clues why this is happening?

Walery Strauch
  • 6,792
  • 8
  • 50
  • 57
Dao Lam
  • 2,837
  • 11
  • 38
  • 44
  • Possible duplicate of [JMockit ERROR - Native library for Attach API not available in this JRE](http://stackoverflow.com/questions/24868592/jmockit-error-native-library-for-attach-api-not-available-in-this-jre) – L. Holanda Oct 13 '15 at 19:43

7 Answers7

50

Go to Java Build Path of your project and change the JRE System Library and make it points to the jdk instead of jre.

Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498
10

It's not tools.jar that you need, but the native library file for the "Attach API": attach.dll (or the Linux/Mac equivalent, attach.so or similar). A plain JRE does not contain this library. Instead, point Eclipse to a JDK installation, which should contain the jre/bin/attach.dll file.

Rogério
  • 16,171
  • 2
  • 50
  • 63
  • Thank you. But how do you point Eclipse to a JDK Installation? I tried Window->Java->Installed JREs->add->directory of jdk. But that didn't help. – Dao Lam Aug 16 '12 at 22:21
  • Remove jre installation path from eclipse 'Installed JREs'( Window->Java->Installed JREs) tab. Point only jdk installation path. – Krishna Roy Feb 26 '20 at 10:17
7

Add the path to attach.dll to your PATH environment variable

savedme
  • 79
  • 2
  • 9
    This answer is correct in the sense that "it works" but the root cause is the use of jre instead of jdk. Accepted answer should be the other one. – L. Holanda Oct 13 '15 at 19:43
4

You have to set agent. You can set VM arguments if you are using eclipse and Args will be like this:

-javaagent:local path to your jmockit jar\jmockit.jar

eg: -javaagent:D:\jmockit.jar

sasuke
  • 595
  • 1
  • 5
  • 15
2

Dao Lam, Here are some other things to try:

  • Make sure your project is using either:
    • The workspace default (That assumes that the default is your the jdk installation you just added. From the window where you added it, make sure it is checked.),
    • The jdk version, or
    • An execution Environment that uses the jdk version.
  • Pull up your system's environment variables, and add this path to it: (jdk_dir)/jre/bin. (Remember, you'll have to restart Eclipse once you've saved that to get it to take).
  • Close eclipse, and modify the shortcut you are using to start it to use (jdk_dir)/jre/bin.

I've found that any one of these measures will allow Eclipse to find the attach.dll.

jchess
  • 81
  • 1
  • 5
2

Looks like you need to point your Eclipse to JDK , not JRE.

Steps :Windows->Preferences>Java>"Installed JRE's" and remove JRE from "Installed JRE's" window and add JDK browsing to your JDK location and select as default.

supernova
  • 3,111
  • 4
  • 33
  • 30
0

I found this issue with JDK 1.8.45 and 1.8.80 on 64-bit Windows. My solution was to copy attach.dll from the JDK's ./jre/bin directory to the ./bin. This eliminated the need for updating Eclipse and a Cygwin command line with a non-standard JAVA_HOME.

Maybe the Windows version may be ignoring the contents of the JRE directory?