简体   繁体   中英

Maven build throws java.lang.NoClassDefFoundError

SOLVED: My class resource was named TestCounter... maven was trying to test it. Renamed it to CounterSwitch and build was successful.

I'm very new to maven, please bear with me... I have some tests that run great in eclipse, but fail during a maven build.

The tests are in: src/test/java/main/*.java

The tests use test-only classes in: src/test/java/**/*.java

The tests also require access to resources located in: src/test/resources/packages/etc...

Within my resources are .java source files that I am not compiling, but am using for JaCoCo analysis tests. I'm loading these resources using relative pathed File objects since resource loader insists on using absolute pathing and causes my tests to fail.

Common sense says that the relative pathing changes post-build and that's why maven's throwing NoClassDefFoundError , but it's also throwing the exception when it encounters one of the .java files... it may be trying to compile it or it may just be a coincidence.

However, the solution may be more convenient as it is not necessary for me to run these tests during the maven lifecycle, they're functional tests that are used more for the results they produce and less for their assertions but I have no idea how to blacklist them.

I have two questions: how do I segregate those files from my maven build that are undesirable, and how do I include those files into my maven build that are?

I'd appreciate any help I can get... here's a relevant representation of my pom.xml (plugins make it quite lengthy) for reference, it's almost a default configuration as I have no idea where to begin with it:

<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>com.genericpackage</groupId>
    <artifactId>genericartifact</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>genericartifact</name>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Stack Trace:

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:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
        at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
        at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
        at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
Caused by: java.lang.NoClassDefFoundError: mockprocesses/itclient-counter/bin/main/TestCounter (wrong name: main/TestCounter)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        at org.apache.maven.surefire.util.DefaultDirectoryScanner.loadClass(DefaultDirectoryScanner.java:98)
        at org.apache.maven.surefire.util.DefaultDirectoryScanner.locateTestClasses(DefaultDirectoryScanner.java:78)
        at org.apache.maven.surefire.junit4.JUnit4Provider.scanClassPath(JUnit4Provider.java:174)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:83)
        ... 9 more

Fixed it, the problem was obvious... the class/java files were named TestCounter... surefire was trying to test them. Renamed them to CounterSwitch and the build went off without a hitch.

Had same issue, fixed it including the file as part of maven-surefire-plugin configuration, as follows:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <includes>
          <include>src/main/java/mockprocesses/itclient-counter/bin/main/TestCounter.java</include>
        </includes>
    </configuration>
</plugin>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM