简体   繁体   English

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

[英]Error in Making Executable Jar file using Maven

This is my pom.xml file of the application 这是我的应用程序的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>

I have setup all the required stuff to make my jar file executable. 我已经设置了所有必需的东西来使我的jar文件可执行。

Following is the error I get once I run the mvn package command on the terminal. 以下是我在终端上运行mvn package命令后得到的错误。 I use Ubuntu as my OS and IdeaJ as my IDE. 我使用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

I know that this exception indicates that Log4J dependency is not existing. 我知道这个异常表明Log4J依赖项不存在。 But It is there in the dependency folder. 但它在依赖文件夹中。

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

As it stands, your MANIFEST.MF file does not tell the JVM where to look for libraries, and that is why you get the exception java.lang.NoClassDefFoundError . 就目前而言,您的MANIFEST.MF文件并不告诉JVM在哪里查找库,这就是您获得异常java.lang.NoClassDefFoundError This is because there is no ClassPath section inside the manifest. 这是因为清单中没有ClassPath部分。

The goals of the MANIFEST.MF file is to store the fully classified name of the main class and the location to all the libraries needed to make the application work. MANIFEST.MF文件的目标是将主类的完全分类名称和位置存储到使应用程序工作所需的所有库中。

In a Maven project, this file is typically generated by the maven-jar-plugin . 在Maven项目中,此文件通常由maven-jar-plugin You need to configure the maven-jar-plugin this way: 您需要以这种方式配置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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM