简体   繁体   中英

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. Here is what I have and what I do :

The content of my pom.xml file :

<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

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 :

$ 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. It looks like this dependency is wrongly include in the final jar file. Of course, I've found a lot of topics about how to create an executable jar file and how to include a .jar file. But none of them solved my problem. There should be something wrong in my pom.xml but what ?

Thanks a lot

MrDomoo

I was not able to use the Top-Q Public repository because the artifact is missing. So I had to install the JAR into my local Maven repository :

Run > Run Configurations > Maven Build (double click)

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 :

<dependency>
    <groupId>net.sourceforge.jpcap</groupId>
    <artifactId>local</artifactId>
    <version>0.01.16</version>
</dependency>

Thanks to deHaar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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