ant 的 junit 任务中的另一个 java.lang.ClassNotFoundException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1215730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:59:38  来源:igfitidea点击:

another java.lang.ClassNotFoundException in ant's junit task

javaantjunit

提问by denchr

I can't figure out why I am getting this exception from my ant build.xml file. I checked and everything is in the classpath. Why must this be so complicated?!

我不明白为什么我从我的 ant build.xml 文件中得到这个异常。我检查了一下,一切都在类路径中。为什么要这么复杂?!

I had trouble with Ant in the past and it seems it always is something related to the classpath. I am pointing to junit.jar using both ways: within eclipse: window->preferences->ant->runtime->Ant Home->Add External Jars, and also within the build.xml script. This time Ant is not able to locate my test class in the junit task. Is there something wrong with the way I am pointing to this class?

过去我在使用 Ant 时遇到了麻烦,似乎它总是与类路径有关。我使用两种方式指向 junit.jar:在 eclipse 中:window->preferences->ant->runtime->Ant Home->Add External Jars,以及在 build.xml 脚本中。这次 Ant 无法在 junit 任务中找到我的测试类。我指向这门课的方式有什么问题吗?

<target name="init">
    <property name="sourceDir" value="src"/>
    <property name="outputDir" value="build" />
    <property name="junitLocation" value="C:\...\org.junit4_4.3.1\junit.jar" /> 
</target>

<target name="clean" depends="init">
    <delete dir="${outputDir}" />
</target>

<target name="prepare" depends="clean">
    <mkdir dir="${outputDir}" />
</target>

<target name="compile" depends="prepare">
     <javac srcdir="${sourceDir}" destdir="${outputDir}" classpath="${junitLocation}"/>
</target>

<path id="classpath">
   <pathelement location="${outputDir}" />
   <pathelement location="${junitLocation}" />
</path>

<target name="testApplication" depends="compile">
    <echo>Running the junit tests...</echo>
    <junit fork="yes" haltonfailure="yes">
        <test name="my.package.MyTest" />
        <formatter type="plain" usefile="false" />      
        <classpath refid="classpath" />
    </junit>
</target>

I am always getting:

我总是得到:

    [junit] Testsuite: my.package.MyTest
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
    [junit]     Caused an ERROR
    [junit] my.package.MyTest
    [junit] java.lang.ClassNotFoundException: my.package.MyTest
    [junit]     at java.net.URLClassLoader.run(Unknown Source)
    [junit]     at java.security.AccessController.doPrivileged(Native Method)
    [junit]     at java.net.URLClassLoader.findClass(Unknown Source)
    [junit]     at java.lang.ClassLoader.loadClass(Unknown Source)
    [junit]     at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    [junit]     at java.lang.ClassLoader.loadClass(Unknown Source)
    [junit]     at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    [junit]     at java.lang.Class.forName0(Native Method)
    [junit]     at java.lang.Class.forName(Unknown Source)

BUILD FAILED

Apparently, Ant finds junit.jar and attempts to start the test, but why can't it find my test class? I point to the folder with compiled test class. So I know that junit is on Ant's classpath at least, but the ClassNotFound puzzles me.

显然,Ant 找到了 junit.jar 并尝试启动测试,但为什么找不到我的测试类?我指向带有编译测试类的文件夹。所以我知道 junit 至少在 Ant 的类路径上,但是 ClassNotFound 使我感到困惑。

Any ideas perhaps? Many thanks!

有什么想法吗?非常感谢!

采纳答案by ChssPly76

Are you sure your test class is in the buildfolder? You're invoking junitin a separate JVM (fork=true) so it's possible that working folder would change during that invocation and with buildbeing relative, that may cause a problem.

您确定您的测试类在build文件夹中吗?您正在junit一个单独的 JVM (fork=true) 中调用,因此在调用期间工作文件夹可能会发生变化并且build是相对的,这可能会导致问题。

Run ant from command line (not from Eclipse) with -verbose or -debug switch to see the detailed classpath / working dir junitis being invoked with and post the results back here if you're still can't resolve this issue.

使用 -verbose 或 -debug 开关从命令行(而不是 Eclipse)运行 ant 以查看junit正在调用的详细类路径/工作目录,如果您仍然无法解决此问题,请将结果发回此处。

回答by Spyder

You don't need to add the JUnit jar to the classpath. If ant can't find it, the <junit>task won't run.

您不需要将 JUnit jar 添加到类路径中。如果 ant 找不到它,<junit>任务将不会运行。

I suggest trying different ways of specifying the classpath as described in the ant doc at http://ant.apache.org/manual/using.html#path; specifically <classpath path="${srcdir}" />might help.

我建议尝试不同的方法来指定类路径,如http://ant.apache.org/manual/using.html#path 上的 ant 文档中所述;具体<classpath path="${srcdir}" />可能有帮助。

If not, ChssPly76's suggestion of running ant -debug from the command line is the best bet. You'll want to bump up the buffer on your command prompt window though, because ant's debugger is extremely verbose.

如果没有,ChssPly76 建议从命令行运行 ant -debug 是最好的选择。不过,您需要在命令提示符窗口上增加缓冲区,因为 ant 的调试器非常冗长。