maven包可执行文件jar无法运行" NoClassDefFoundError"

时间:2016-04-22 12:23:30

标签: java maven

我使用maven maven package命令编译了我的项目,并导出了成功创建的可执行文件jar文件。

但是当我运行它时,只输出错误NoclassDefFoundError

$ java -server -Xms512m -Xmx2048m -Dlogback.configurationFile=`pwd`/resources/logback.xml -jar stocktalk-chat-0.3.1.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: io/netty/channel/EventLoopGroup
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.privateGetMethodRecursive(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: io.netty.channel.EventLoopGroup
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 7 more

在我的宣言中,

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: Administrator
Class-Path: lib/aws-java-sdk-core-1.10.68.jar lib/commons-logging-1.1.
 3.jar lib/httpclient-4.3.6.jar lib/httpcore-4.3.3.jar lib/commons-cod
 ec-1.6.jar lib/jackson-databind-2.5.3.jar lib/jackson-annotations-2.5
 .0.jar lib/jackson-core-2.5.3.jar lib/jackson-dataformat-cbor-2.5.3.j
 ar lib/joda-time-2.8.1.jar lib/aws-java-sdk-datapipeline-1.10.68.jar 
 lib/aws-java-sdk-dynamodb-1.10.68.jar lib/aws-java-sdk-s3-1.10.68.jar
  lib/aws-java-sdk-kms-1.10.68.jar lib/gcm-server-1.0.2.jar lib/json-s
 imple-1.1.jar lib/slf4j-api-1.7.7.jar lib/logback-classic-1.1.2.jar l
 ib/logback-core-1.1.2.jar lib/jcl-over-slf4j-1.7.7.jar lib/log4j-api-
 2.5.jar lib/log4j-core-2.5.jar lib/mybatis-3.3.1.jar lib/netty-all-4.
 0.36.Final.jar lib/lombok-1.16.8.jar lib/gson-2.6.2.jar lib/jtds-1.3.
 1.jar
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_65
Main-Class: kr.stocktalk.chat.ChatServer

在Class-Path中添加了所有依赖项。

请检查我的pom.xml。有什么问题?

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>
    <modelVersion>4.0.0</modelVersion>
    <groupId>kr.stocktalk</groupId>
    <artifactId>stocktalk-chat</artifactId>
    <version>0.3.1</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>kr.stocktalk.chat.ChatServer</mainClass>
                    <arguments>
                        <argument>-server</argument>
                        <argument>-Xms512m</argument>
                        <argument>-Xmx2048m</argument>
                        <argument>-Dlogback.configurationFile=classpath:resources/logback.xml</argument>
                    </arguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <!-- <compilerArgument>-Xlint:all</compilerArgument> -->
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> 
                <version>2.3</version> <executions> <execution> <id>copy-dependencies</id> 
                <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> </execution> 
                </executions> <configuration> eclipse 에서는 target 밑의 lib 에 생성 됨 <outputDirectory>lib</outputDirectory> 
                <overWriteIfNewer>true</overWriteIfNewer> </configuration> </plugin> -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>kr.stocktalk.chat.ChatServer</mainClass>
                            <classpathPrefix>lib</classpathPrefix>
                        </manifest>
                        <!-- <manifestEntries>
                            <Class-Path>lib/</Class-Path>
                        </manifestEntries> -->
                    </archive>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <targetPath>resources</targetPath>
            </resource>
        </resources>
    </build>
    <dependencies>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-core</artifactId>
            <version>1.10.68</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-datapipeline</artifactId>
            <version>1.10.68</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-dynamodb</artifactId>
            <version>1.10.68</version>
        </dependency>
        <dependency>
            <groupId>com.ganyo</groupId>
            <artifactId>gcm-server</artifactId>
            <version>1.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.7</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.2</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.7.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.3.1</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.0.36.Final</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.8</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.6.2</version>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.jtds</groupId>
            <artifactId>jtds</artifactId>
            <version>1.3.1</version>
        </dependency>
    </dependencies>
</project>

1 个答案:

答案 0 :(得分:1)

虽然我不是Maven的专家,但我认为您需要使用<outputdirectory>设置相同的<classpathPrefix>路径值。

所以,你可以像这样修改。

 <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>kr.stocktalk.chat.ChatServer</mainClass>
                        <classpathPrefix>classes/lib</classpathPrefix>
                    </manifest>
                    <!-- <manifestEntries>
                        <Class-Path>lib/</Class-Path>
                    </manifestEntries> -->
                </archive>
            </configuration>
        </plugin>

请检查此部分 <classpathPrefix>classes/lib</classpathPrefix>

然后你的MANIFIEST文件的Class-Path就像这样。

Class-Path: classes/lib/netty-all-4.0.36.Final.jar