Maven:主线程中的NoClassDefFoundError

时间:2022-09-03 22:53:08

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:

我目前正在构建一个Apache-Mina Server应用程序。我正在使用Maven来构建它。当我尝试运行jar时,我收到以下错误:

    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的样子:

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:

这是我的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?

我在这做错了什么?

2 个解决方案

#1


7  

When you run from Eclipse, Eclipse configures the class path for you. Therefore, you don't run into this issue.

从Eclipse运行时,Eclipse会为您配置类路径。因此,您不会遇到此问题。

When you are running outside of Eclipse, you need to set up the CLASSPATH either by providing the path to these jar files ie file:/dev/libs/mina-core-2.0.3.jar in the MANIFEST.MF or by adding the -cp option when executing the app. Don't forget that the entries in the class-path in the manifest file are either relative to the JAR in which they are embedded or absolute path to a local file directory.

当您在Eclipse之外运行时,您需要通过提供这些jar文件的路径来设置CLASSPATH,即文件:MANIFEST.MF中的文件:/dev/libs/mina-core-2.0.3.jar或者添加执行应用程序时的-cp选项。不要忘记清单文件中类路径中的条目要么是相对于嵌入它们的JAR,要么是本地文件目录的绝对路径。

Your other option is to package it as one jar using the maven assembly plugin jar-with-dependencies.

您的另一个选择是使用maven程序集插件jar-with-dependencies将其打包为一个jar。

#2


15  

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.

另一种选择是使用maven-dependency-plugin。您可以将所有依赖库复制到lib等文件夹,并将其用于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.

对于类路径,这里classpathPrefix指定所有依赖项应位于相对于存档的“lib”文件夹中。

<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

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

#1


7  

When you run from Eclipse, Eclipse configures the class path for you. Therefore, you don't run into this issue.

从Eclipse运行时,Eclipse会为您配置类路径。因此,您不会遇到此问题。

When you are running outside of Eclipse, you need to set up the CLASSPATH either by providing the path to these jar files ie file:/dev/libs/mina-core-2.0.3.jar in the MANIFEST.MF or by adding the -cp option when executing the app. Don't forget that the entries in the class-path in the manifest file are either relative to the JAR in which they are embedded or absolute path to a local file directory.

当您在Eclipse之外运行时,您需要通过提供这些jar文件的路径来设置CLASSPATH,即文件:MANIFEST.MF中的文件:/dev/libs/mina-core-2.0.3.jar或者添加执行应用程序时的-cp选项。不要忘记清单文件中类路径中的条目要么是相对于嵌入它们的JAR,要么是本地文件目录的绝对路径。

Your other option is to package it as one jar using the maven assembly plugin jar-with-dependencies.

您的另一个选择是使用maven程序集插件jar-with-dependencies将其打包为一个jar。

#2


15  

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.

另一种选择是使用maven-dependency-plugin。您可以将所有依赖库复制到lib等文件夹,并将其用于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.

对于类路径,这里classpathPrefix指定所有依赖项应位于相对于存档的“lib”文件夹中。

<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

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