Following exception occurs in Windows for IBM JDK 6 while executing a JUnit testcase which uses JMockit framework, in-spite of enabling the Attach API "-Dcom.ibm.tools.attach.enable=yes" - java.lang.RuntimeException: com.sun.tools.attach.AttachNotSupportedException: Unable to enqueue operation: the target VM does not support attach mechanism

Tools and Technologies used in this article

  1. JMockit
  2. JUnit 3
  3. IBM JDK 6
  4. IBM Rational Application Developer
  5. Windows 7 OS

Problem

Console

java.lang.RuntimeException: com.sun.tools.attach.AttachNotSupportedException: Unable to enqueue operation: the target VM does not support attach mechanism
      at mockit.internal.startup.JDK6AgentLoader.getVirtualMachineImplementationFromEmbeddedOnes(JDK6AgentLoader.java:89)
      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:144)
      at mockit.internal.startup.Startup.initializeIfPossible(Startup.java:177)
      at junit.framework.TestResult.<clinit>(TestResult.java:15)
      at java.lang.J9VMInternals.initializeImpl(Native Method)
      at java.lang.J9VMInternals.initialize(J9VMInternals.java:200)
      at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:121)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: com.sun.tools.attach.AttachNotSupportedException: Unable to enqueue operation: the target VM does not support attach mechanism
      at sun.tools.attach.WindowsVirtualMachine.<init>(WindowsVirtualMachine.java:64)
      at mockit.internal.startup.JDK6AgentLoader.getVirtualMachineImplementationFromEmbeddedOnes(JDK6AgentLoader.java:73)

Note : You may get the error in-spite of enabling Attach API -Dcom.ibm.tools.attach.enable=yes.

-Dcom.ibm.tools.attach.enable=yes

The Attach API allows your application to connect to a virtual machine. Your application can then load an agent application into the virtual machine. The agent can be used to perform tasks such as monitoring the virtual machine status

Fix

Follow the steps below

  1. Download jmockit-1.6 and extract to a folder (say c:\jmockit).
  2. Select Run --> Run Configurations... --> JUnit --> Arguments and add VM arguments "-javaagent:jmockit.jar".
    Run Configurations - VM Arguments
  3. Execute JUnit test case.

References