23
votes

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?

7

7 Answers

48
votes

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

10
votes

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.

7
votes

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

4
votes

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

2
votes

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.

2
votes

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.

0
votes

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?