Maven编译导致ClassNotFoundException

时间:2016-06-09 17:00:11

标签: java maven guava

编译并在主类的CMD中启动后,我得到以下错误:

java.lang.reflect.InvocationTargetException
Caused by: java.lang.RuntimeException: Exception in Application start method
Caused by: java.lang.NoClassDefFoundError: com/google/common/collect/RangeSet
Caused by: java.lang.ClassNotFoundException: com.google.common.collect.RangeSet

P.S。在编译期间没有错误。只执行失败。同样在Intellij我运行项目没有问题。 在POM我有

<dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>19.0</version>
        <scope>compile</scope>
    </dependency>

在构建中添加guava似乎是个问题。我是Maven的新手。怎么解决这个问题?

       "C:\Program Files\Java\jdk1.8.0_74\bin\java" -Dmaven.multiModuleProjectDirectory=D:\GitHub\TestPlatform "-Dmaven.home=C:\Program Files\apache-maven-3.3.9" "-Dclassworlds.conf=C:\Program Files\apache-maven-3.3.9\bin\m2.conf" -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.1.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\apache-maven-3.3.9\boot\plexus-classworlds-2.5.2.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.1.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain org.codehaus.classworlds.Launcher -Didea.version=2016.1.1 compile
    [INFO] Scanning for projects...
    [INFO]                                                                         
    [INFO] ------------------------------------------------------------------------
    [INFO] Building TestPlatform 0.1-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO] 
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ TestPlatform ---
    [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] Copying 7 resources
    [INFO] 
    [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ TestPlatform ---
    [INFO] Changes detected - recompiling the module!
    [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
    [INFO] Compiling 13 source files to D:\GitHub\TestPlatform\target\classes
    [WARNING] /D:/GitHub/TestPlatform/src/main/java/com/cr/testplatform/controller/CommandCenterController.java: Some input files use unchecked or unsafe operations.
    [WARNING] /D:/GitHub/TestPlatform/src/main/java/com/cr/testplatform/controller/CommandCenterController.java: Recompile with -Xlint:unchecked for details.
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 1.687 s
    [INFO] Finished at: 2016-06-09T22:16:34+05:00
    [INFO] Final Memory: 15M/298M
    [INFO] ------------------------------------------------------------------------

    Process finished with exit code 0

    D:\GitHub\TestPlatform>mvn dependency:tree
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building TestPlatform 0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ TestPlatform ---
[INFO] com.cr:TestPlatform:jar:0.1-SNAPSHOT
[INFO] +- com.google.guava:guava:jar:19.0:compile
[INFO] +- io.netty:netty-all:jar:4.1.0.Final:compile
[INFO] +- com.google.protobuf:protobuf-java:jar:2.5.0:compile
[INFO] +- org.eclipse.jetty.npn:npn-api:jar:1.1.1.v20141010:compile
[INFO] \- junit:junit:jar:4.11:test
[INFO]    \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.067 s
[INFO] Finished at: 2016-06-09T22:25:21+05:00
[INFO] Final Memory: 13M/368M
[INFO] ------------------------------------------------------------------------

2 个答案:

答案 0 :(得分:2)

默认情况下,maven只将您的文件打包到jar中。有几种不同的方法可以将依赖项打包到jar中,涵盖here

更简单的解决方案之一是包含程序集插件:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>my.fully.qualified.class.Main</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-my-jar-with-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

这将构建两个jar:一个只包含你的类文件,另一个(名为my-project-VERSION-jar-with-dependencies.jar)包含所有下游依赖项。

答案 1 :(得分:0)

删除&#34;范围&#34; line - 它告诉Maven在编译期间只包含该依赖项,但它看起来像是一个运行时依赖项。

编辑 - 该死的 - @ mfulton26是对的。我不得不重新阅读一些Maven文档,意识到我错了。对不起。