[英]Create executable jar with maven in eclipse - java.lang.NoClassDefFoundError
Since now a week, I'm trying to create an executable jar with maven in eclipse but I'm facing troubles. 从现在开始一个星期以来,我试图用Eclipse中的maven创建一个可执行jar,但是我遇到了麻烦。 Here is what I have and what I do : 这是我拥有的和我做的:
The content of my pom.xml file : 我的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">
<modelVersion>4.0.0</modelVersion>
<groupId>B4D</groupId>
<artifactId>B4D</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>B4D</name>
<description>Bot for Dofus</description>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>Cp1252</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>fr.B4D.gui.JFrame_B4D</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.jgrapht</groupId>
<artifactId>jgrapht-core</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.28.0</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>1.23.0</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-sheets</artifactId>
<version>v4-rev493-1.23.0</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-sqladmin</artifactId>
<version>v1beta4-rev66-1.25.0</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-drive</artifactId>
<version>v3-rev136-1.25.0</version>
</dependency>
<dependency>
<groupId>com.github.kirviq</groupId>
<artifactId>dumbster</artifactId>
<version>1.7.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.jpcap</groupId>
<artifactId>capture</artifactId>
<version>0.01.16</version>
<scope>system</scope>
<systemPath>${project.basedir}/jpcap/net.sourceforge.jpcap-0.01.16.jar</systemPath>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
I build my project by doing : 我通过以下方式构建我的项目:
Project > Run As > Maven build > Goals : package > Run 项目>运行方式> Maven构建>目标:包>运行
And here is the console output : 这是控制台输出:
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for B4D:B4D:jar:1.0
[WARNING] 'dependencies.dependency.systemPath' for net.sourceforge.jpcap:capture:jar should not point at files within the project directory, ${project.basedir}/jpcap/net.sourceforge.jpcap-0.01.16.jar will be unresolvable by dependent projects @ line 115, column 15
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building B4D 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ B4D ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 32 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ B4D ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 84 source files to D:\Lucas\Projets personnels\Eclipse Windows\B4D\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ B4D ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ B4D ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 7 source files to D:\Lucas\Projets personnels\Eclipse Windows\B4D\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ B4D ---
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ B4D ---
[INFO] Building jar: D:\Lucas\Projets personnels\Eclipse Windows\B4D\target\B4D-1.0.jar
[INFO]
[INFO] --- maven-assembly-plugin:2.2-beta-5:single (make-assembly) @ B4D ---
[INFO] META-INF/ already added, skipping
[INFO] META-INF/MANIFEST.MF already added, skipping
................
[INFO] com/google/api/ already added, skipping
[INFO] com/google/api/services/ already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 02:01 min
[INFO] Finished at: 2019-03-14T16:54:57+01:00
[INFO] Final Memory: 27M/307M
[INFO] ------------------------------------------------------------------------
And when I try to run the jar file in a console : 当我尝试在控制台中运行jar文件时:
$ java -jar B4D-1.0-jar-with-dependencies.jar
Exception in thread "main" java.lang.NoClassDefFoundError: net/sourceforge/jpcap /capture/InvalidFilterException
at fr.B4D.gui.JFrame_B4D.<init>(JFrame_B4D.java:64)
at fr.B4D.gui.JFrame_B4D.main(JFrame_B4D.java:55)
Caused by: java.lang.ClassNotFoundException: net.sourceforge.jpcap.capture.Inval idFilterException
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)
... 2 more
Note that my project use a local jar file dependency : net.sourceforge.jpcap-0.01.16.jar. 请注意,我的项目使用本地jar文件依赖项:net.sourceforge.jpcap-0.01.16.jar。 It looks like this dependency is wrongly include in the final jar file. 看起来这个依赖项错误地包含在最终的jar文件中。 Of course, I've found a lot of topics about how to create an executable jar file and how to include a .jar file. 当然,我发现了很多关于如何创建可执行jar文件以及如何包含.jar文件的主题。 But none of them solved my problem. 但他们都没有解决我的问题。 There should be something wrong in my pom.xml but what ? 我的pom.xml应该有问题,但是什么呢?
Thanks a lot 非常感谢
MrDomoo MrDomoo
I was not able to use the Top-Q Public repository because the artifact is missing. 我无法使用Top-Q公共存储库,因为缺少工件。 So I had to install the JAR into my local Maven repository : 所以我不得不将JAR安装到我的本地Maven存储库中:
Run > Run Configurations > Maven Build (double click) 运行>运行配置> Maven构建(双击)
The goal is : 目标是:
install:install-file -Dfile=${basedir}/src/main/resources/net.sourceforge.jpcap-0.01.16.jar -DgroupId=net.sourceforge.jpcap -DartifactId=local -Dversion=0.01.16 -Dpackaging=jar
I can now in the pom.xml, simply use : 我现在可以在pom.xml中,只需使用:
<dependency>
<groupId>net.sourceforge.jpcap</groupId>
<artifactId>local</artifactId>
<version>0.01.16</version>
</dependency>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.