Dao Lam
Dao Lam

Reputation: 2937

jmockit: Native library for Attach API not available in this JRE error

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?

Upvotes: 25

Views: 30377

Answers (8)

ZhengguanLi
ZhengguanLi

Reputation: 131

I am running a maven project in IntelliJ, I pretty sure I am using JDK instead of JRE. Finally I found the solution for me is to configure the -javaagent for jmockit in maven-surefire-plugin as following:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <argLine>-javaagent:"${settings.localRepository}"/org/jmockit/jmockit/1.14/jmockit-1.14.jar</argLine>
    </configuration>
</plugin>

Upvotes: 0

supernova
supernova

Reputation: 3261

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.

Upvotes: 2

user5601840
user5601840

Reputation: 1

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?

Upvotes: 0

Mahmoud Saleh
Mahmoud Saleh

Reputation: 33605

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

Upvotes: 50

jchess
jchess

Reputation: 81

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.

Upvotes: 2

sasuke
sasuke

Reputation: 615

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

Upvotes: 5

savedme
savedme

Reputation: 79

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

Upvotes: 7

Rog&#233;rio
Rog&#233;rio

Reputation: 16380

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.

Upvotes: 10

Related Questions