使用Maven制作可执行Jar文件时出错

时间:2015-03-05 06:51:13

标签: java maven logging

这是我的应用程序的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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.smart.data.manipulator</groupId>
    <artifactId>SmartDataManipulator</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>SmartDataManipulator</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- Dependency for Logger -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!-- Dependency for MySQL JDBC Driver -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>SmartDataManipulator</finalName>
        <plugins>
            <!-- Set a JDK compiler level -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <!-- Make this jar executable -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <!-- Jar file entry point -->
                            <mainClass>com.smart.data.manipulator.Starter</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <!-- Copy project dependency -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.5.1</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

我已经设置了所有必需的东西来使我的jar文件可执行。

以下是我在终端上运行mvn package命令后得到的错误。我使用 Ubuntu 作为我的操作系统,使用 IdeaJ 作为我的IDE。

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
    at com.smart.data.manipulator.Starter.<clinit>(Starter.java:23)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 1 more

我知道此异常表明Log4J依赖项不存在。但它存在于依赖文件夹中。

junit-3.8.1.jar  log4j-1.2.17.jar  mysql-connector-java-5.1.34.jar

1 个答案:

答案 0 :(得分:0)

就目前而言,您的MANIFEST.MF文件不会告诉JVM在哪里查找库,这就是您获得异常java.lang.NoClassDefFoundError的原因。这是因为清单中没有ClassPath部分。

MANIFEST.MF文件的目标是将主类的完全分类名称和位置存储到使应用程序工作所需的所有库中。

在Maven项目中,此文件通常由maven-jar-plugin生成。您需要以这种方式配置maven-jar-plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <!-- Jar file entry point -->
                <mainClass>com.smart.data.manipulator.Starter</mainClass>
                <!-- Adds a ClassPath section to the manifest -->                    
                <addClasspath>true</addClasspath>
                <!-- All the dependencies are stored inside the dependency folder, as created by default by the maven-dependency-plugin -->
                <classpathPrefix>dependency/</classpathPrefix> 
            </manifest>
        </archive>
    </configuration>
</plugin>