我正在运行一个批处理的小型python脚本,该脚本可以通过运行其所有Junit测试来自动对一堆Java程序进行评分。我使用导出的Eclipse项目,将其解压缩,然后调用maven运行测试。我没有对原始Eclipse项目进行任何更改,并且测试在Eclipse中运行良好。

这是我第一次使用Maven。

作为参考,我在Windows 10上运行,并且我的路径都已正确设置,并且我相信它们与Eclipse中发生的情况匹配。

Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-

10T11:41:47-05:00)
Maven home: C:\Program Files\Apache\apache-maven-3.3.9
Java version: 1.8.0_111, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.8.0_111\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "dos"


我收到以下错误:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
org.apache.maven.surefire.util.SurefireReflectionException: java.lang.reflect.InvocationTargetException; nested exception is java.lang.reflect.InvocationTargetException: null
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: java.lang.NoClassDefFoundError: edu/brandeis/cs12b/PA7/PA7/target/test-classes/edu/brandeis/cs12b/PA7/KnowledgeBaseTest (wrong name: edu/brandeis/cs12b/PA7/KnowledgeBaseTest)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.apache.maven.surefire.util.DefaultScanResult.loadClass(DefaultScanResult.java:131)
    at org.apache.maven.surefire.util.DefaultScanResult.applyFilter(DefaultScanResult.java:95)
    at org.apache.maven.surefire.junit4.JUnit4Provider.scanClassPath(JUnit4Provider.java:194)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:92)
    ... 9 more

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0


这是POM文件的副本:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>edu.brandeis.cs12b.pa7</groupId>
    <artifactId>PA7</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>PA7</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
</project>

最佳答案

造成原因:java.lang.NoClassDefFoundError:
  edu / brandeis / cs12b / PA7 / PA7 / target / test-classes / edu / brandeis / cs12b / PA7 / KnowledgeBaseTest
  (错误名称:edu / brandeis / cs12b / PA7 / KnowledgeBaseTest)


这很奇怪:您在项目中似乎有两次PA7程序包,但在类的程序包中却没有。
这可能是您的问题,因为Maven将wrong name称为错误。

此外,应避免在包:edu/brandeis/cs12b/PA7中使用大写字母。它与Java约定相反。

07-24 21:10