gpt4 book ai didi

java.lang.ClassNotFoundException : main. java.KmToMileTest

转载 作者:行者123 更新时间:2023-11-30 07:24:09 24 4
gpt4 key购买 nike

我在 OSX 终端中使用 Ant 运行 JUnit 测试时遇到问题。我知道问题一定出在类路径上,或者我弄乱了我的文件目录,请帮忙!

我遇到的错误:

test1:
[junit] Running main.java.KmToMileTest
[junit] Testsuite: main.java.KmToMileTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
[junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
[junit]
[junit] Caused an ERROR
[junit] main.java.KmToMileTest
[junit] java.lang.ClassNotFoundException: main.java.KmToMileTest
[junit] at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[junit] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:264)
[junit]
[junit] Test main.java.KmToMileTest FAILED

BUILD SUCCESSFUL

这是我的 build.xml:

<project name="MyProject" default="test1" basedir=".">
<description>
Ant build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="bin" location="bin"/>
<property name="junit" location="junit/"/>

<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory -->
<mkdir dir="${bin}"/>
</target>

<target name="compile" depends="init"
description="compile the source " >
<!-- Compile the java code from ${src} into ${bin} -->
<javac includeantruntime="false"
srcdir="${src}"
destdir="${bin}"
debug="on">
<classpath location="${junit}/junit-4.11.jar"/>
<classpath location="${junit}/hamcrest-all-1.3.jar"/>
</javac>
</target>

<target name="test1" depends="compile">
<!-- Run junit tests -->
<junit printsummary="yes" fork="yes">
<classpath location="${bin}"/>
<classpath location="${junit}/junit-4.11.jar"/>
<classpath location="${junit}/hamcrest-all-1.3.jar"/>
<formatter type="plain" usefile="false" /> <!-- to screen -->
<formatter type="plain" /> <!-- to file -->

<test name="main.java.KmToMileTest"/>
</junit>
</target>

<target name="clean">
<!-- Delete the ${bin} folder -->
<delete dir="${bin}"/>
</target>

</project>

我的文件目录:

project:
build.xml
junit/
hamcrest-all-1.3.jar
junit-4.11.jar
bin/prject/
lengthConverter.class
kmToMilesTest.class
milesToKmTest.class
parameterizedTest.class
src/main/java/
lengthConverter.java
kmToMilesTest.java
milesToKmTest.java
parameterizedTest.java

最佳答案

我发现了一些问题:

  1. 您将编译器的源目录设置为 src ,而它应该是 src/main/java
  2. 您的包与您的文件夹结构不匹配(kmToMilesTest 似乎有一个 prject 的包(这就是为什么这些类位于 bin/prject 而不是 bin 中),但不在 src/main/java/prject 文件夹中)。
  3. 您的测试名称不匹配;您尝试运行测试类 main.java.KmToMileTest ,但您的测试类名为 prject.kmToMilesTest

关于java.lang.ClassNotFoundException : main. java.KmToMileTest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37038603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com