在ant中设置javaagent

时间:2011-03-18 14:08:38

标签: java ant junit jmockit

我正在尝试从Ant脚本运行JUnit测试。测试使用JMockit模拟框架,对于Java 5,它需要将其指定为javaagent才能正确运行。这是我正在运行的脚本:

<!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>

我感觉我没有正确设置javaagent。此异常的测试错误:

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(InternalAntRunner.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)

我的javaagent设置是否正确?如果是,还有什么可能导致此错误?

2 个答案:

答案 0 :(得分:0)

我不知道这是否是解决方案,但您没有正确设置类路径。试试这个:

<classpath>
    <pathelement location="${LIB_PATH}/jmockit.jar" />
    <pathelement location="${LIB_PATH}/junit-4.8.2.jar" />
</classpath>

答案 1 :(得分:0)

jvmarg的{​​{1}}到Ant看起来是正确的。您使用的是Java 6还是支持Attach API的JVM?看起来您还必须通过javaagent为旧版本的Java启用它。我假设你需要它,因为异常说“目标VM不支持附加机制”。

对于IBM JDK 6,您似乎需要指定-Dcom.sun.management.jmxremote