Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

java - Maven: NoClassDefFoundError in the main thread

I am currently building a little Apache-Mina Server app. I am using Maven to build it. When i try to run the jar, I get the following error:

    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/mina/filter/codec/ProtocolCodecFactory 
Caused by: java.lang.ClassNotFoundException: org.apache.mina.filter.codec.Protoc  olCodecFactory
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: de.fr1zle.gpsserver.GpsServer. Program will exit.

Running in eclipse is not a problem.

This is what the generated MANIFEST looks like:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: fr1zle
Build-Jdk: 1.6.0_23
Main-Class: de.fr1zle.gpsserver.GpsServer
Class-Path: commons-lang-2.1.jar plexus-utils-1.1.jar junit-4.8.2.jar 
 log4j-1.2.14.jar slf4j-jdk14-1.5.11.jar slf4j-api-1.5.11.jar antlr-2.
 7.6.jar commons-collections-3.1.jar dom4j-1.6.1.jar hibernate-commons
 -annotations-3.2.0.Final.jar hibernate-jpa-2.0-api-1.0.0.Final.jar jt
 a-1.1.jar hibernate-annotations-3.5.6-Final.jar hibernate-core-3.5.6-
 Final.jar mysql-connector-java-5.1.15.jar mina-core-2.0.3.jar

And this is (part of) my pom.xml:

<groupId>de.fr1zle.gpsserver</groupId>
    <artifactId>GPSServer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>GPSServer</name>
    <packaging>jar</packaging>
    <description>Tracks location of GPS modules and the information they submit.</description>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>de.fr1zle.gpsserver.GpsServer</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

What am I doing wrong here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Another option is to use maven-dependency-plugin. You can copy all dependent libraries to a folder such as lib, and use that for classpath.

In order to copy dependencies:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>
                    ${project.build.directory}/lib
                </outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

and for the classpath, here classpathPrefix specifies that all dependencies should be located in a "lib" folder relative to the archive.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>com.citusdata.hadoop.HadoopTest</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

For further information:

http://www.ibm.com/developerworks/java/library/j-5things13/index.html http://maven.apache.org/plugins/maven-dependency-plugin/usage.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...