0

I just finished my first application and I now would like to create one executable jar that I can send to other people so they can use the application I wrote.

I might me completely off course. If so please inform me what I am doing wrong.

I am running the following command in my command prompt (cmd): mvn clean package. This is not giving me any errors.

When I try to run the application using: java -jar AppName.jar. It executes but gives the following error while running:

Exception in thread "main" java.lang.NoClassDefFoundError: net/lingala/zip4j/exception/ZipException
        at CompressedFileOpener.CompressedFileOpener.CompressedOpenerFactory.getOpener(CompressedOpenerFactory.java:12)
        at CompressedFileOpener.CompressedFileOpener.App.main(App.java:15)
Caused by: java.lang.ClassNotFoundException: net.lingala.zip4j.exception.ZipException
        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

My pom.xml file looks like:

<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>CompressedFileOpener</groupId>
  <artifactId>CompressedFileOpener</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>CompressedFileOpener</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>net.lingala.zip4j</groupId>
      <artifactId>zip4j</artifactId>
      <version>1.3.2</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <plugin>
          <!-- Build an executable JAR -->
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.1.0</version>
          <configuration>
            <archive>
              <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>CompressedFileOpener.CompressedFileOpener.App</mainClass>
              </manifest>
            </archive>
          </configuration>
        </plugin>
    </plugins>
  </build>
</project>

Edit: I managed to solve my issue by using the comment section below this post and searching for fat jar.

Adding the follwing to my build path in my pom.xml file created one single jar containing all dependencies as well.

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                  <manifest>
                    <mainClass>CompressedFileOpener.CompressedFileOpener.App</mainClass>
                  </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>

            </configuration>
            <executions>
                <execution>
                    <id>assemble-all</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
Piet Hein
  • 184
  • 2
  • 16
  • When you run `java -jar AppName.jar` does the `lib` directory (and the dependent Jars) exist in the same directory? – MadProgrammer Aug 06 '18 at 22:53
  • I am guessing they are not. This is the first time I am using Maven so I had to Google around a bit to even get it to compile. How would I check this? Is it not possible to create a single jar that contains all the required classes including those that come from dependencies? – Piet Hein Aug 06 '18 at 22:59
  • *"Is it not possible to create a single jar that contains all the required classes including those that come from dependencies?"* - You're looking for "fat jar" - just beware that this can cause some issues if you have shared resources within the same path locations in the other dependencies. To check if the `lib` exists, just list the directory contents and see – MadProgrammer Aug 06 '18 at 23:02
  • 2
    Possible duplicate of [How can I create an executable JAR with dependencies using Maven?](https://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven) – SiKing Aug 07 '18 at 00:11
  • I managed to do what I wanted after a google search for fat jar and edited the original so post so it includes the method that worked for me. Thanksfor the help MadProgrammer! – Piet Hein Aug 07 '18 at 08:40

0 Answers0