Setting javaagent in ant
I am trying to run JUnit tests from an Ant script. The tests use the JMockit mocking framework, which for Java 5 requires specifying it as a javaagent to run correctly. Here is the script I am running:
<!DOCTYPE project>
<project name="junit_test">
<property name="PROJECT_PATH" value="{Path to my eclipse project}" />
<property name="LIB_PATH" value="${PROJECT_PATH}/WebContent/WEB-INF/lib" />
<property name="TEST_PATH" value="WebContent/WEB-INF/classes" />
<target name="run_junit">
<junit fork="yes" forkmode="once" printsummary="true">
<jvmarg value="-javaagent:${LIB_PATH}/jmockit.jar" />
<classpath path="${LIB_PATH}/jmockit.jar" />
<classpath path="${LIB_PATH}/junit-4.8.2.jar" />
<batchtest>
<fileset dir="${TEST_PATH}">
<include name="**/*Test.class"/>
</fileset>
</batchtest>
</junit>
<junitreport todir="/junitOut">
<fileset dir="/junitOut">
<include name="INCOMPLETE-*.xml"/>
<include name="TEST-*.xml"/>
</fileset>
<report todir="/junitOut/html"/>
</junitreport>
</target>
</project>
I have a feeling that I'm not setting the javaagent correctly. The tests error with this exception:
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Constructor.java:515)
at org.eclipse.ant.internal.ui.antsupport.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.run(InternalAntRunner.java:423)
at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.main(InternalAnt开发者_JAVA百科Runner.java:137)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:140)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:140)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:167)
Caused by: 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.attachToThisVM(JDK6AgentLoader.java:113)
at mockit.internal.startup.JDK6AgentLoader.loadAgent(JDK6AgentLoader.java:77)
at mockit.internal.startup.AgentInitialization.initializeAccordingToJDKVersion(AgentInitialization.java:41)
at mockit.internal.startup.Startup.initializeIfNeeded(Startup.java:203)
at java.lang.J9VMInternals.initializeImpl(Native Method)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:200)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:167)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:167)
Caused by: com.sun.tools.attach.AttachNotSupportedException: Unable to enqueue operation: the target VM does not support attach mechanism
at sun.tools.attach.WindowsVirtualMachine.(WindowsVirtualMachine.java:58)
at sun.tools.attach.WindowsAttachProvider.attachVirtualMachine(WindowsAttachProvider.java:58)
at com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:207)
at mockit.internal.startup.JDK6AgentLoader.attachToThisVM(JDK6AgentLoader.java:110)
Is my javaagent setting correct? If it is, what else could be causing this error?
I don't know if that's the solution, but you are not setting the classpath correctly. Try this:
<classpath>
<pathelement location="${LIB_PATH}/jmockit.jar" />
<pathelement location="${LIB_PATH}/junit-4.8.2.jar" />
</classpath>
Your jvmarg
to Ant
for javaagent
looks correct. Are you using Java 6 or a JVM that otherwise supports the Attach API? It looks like you have to also pass -Dcom.sun.management.jmxremote
to enable it for older versions of Java. I'm assuming you need it because the exception says "the target VM does not support attach mechanism".
For IBM JDK 6 it appears you need to specify -Dcom.ibm.tools.attach.enable=yes
.
精彩评论