Maven compile cause ClassNotFoundException

After compile and start in CMD of main class I get the following errors:

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. during compile no Errors. Only execution fails. Also in Intellij I run the project with no problem. In POM i have

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

It seems that it is a problem to add guava in the build. I'm new to Maven. How to solve this?

       "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] ------------------------------------------------------------------------

Upvotes: 1

Views: 2044

Answers (2)

Andrew Rueckert
Andrew Rueckert

Reputation: 5215

By default, maven only packages your files into the jar. There are a couple of different ways of getting it to package dependencies into the jar as well, covered here.

One of the easier solutions is to include the assembly plugin:

<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>

This will build two jars: one that only has your class files in it, and one (named my-project-VERSION-jar-with-dependencies.jar) that includes all of your downstream dependencies.

Upvotes: 3

stdunbar
stdunbar

Reputation: 17435

Remove the "scope" line - it is telling Maven to only include that dependency during compile time but it looks like it is a run time dependency.

EDIT - damn - @mfulton26 is right. I had to re-read some Maven docs to realize that I had it wrong. Sorry about that.

Upvotes: 0

Related Questions