Maven:主线程中的NoClassDefFoundError [英] Maven: NoClassDefFoundError in the main thread

查看:157
本文介绍了Maven:主线程中的NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

在eclipse中运行不是问题。

Running in eclipse is not a problem.

这就是生成的MANIFEST的样子:

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

这是我的pom.xml的一部分:

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>

我在这里做错了什么?

推荐答案

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

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

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

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.

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

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

这篇关于Maven:主线程中的NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆