'When I Eclipse project to maven, Why can't I export a .jar without "Fat Jar Export: Could not find class-path for ...target/test-classes

I had a working Eclipse project for which I could export a .jar file with no problems. I then did a 'configure/convert to maven'. Now, when I try to export the .jar file I get the error message:

Fat Jar Export: Could not find class-path entry for 'Users/ward/dropbox/workspaces/newautomation/workspace/automation/target/test-classes'.

Although it does write the .jar file, the .jar file does not work properly.

Any help? ward



Solution 1:[1]

As stated in my question, I had a project running in Eclipse and could successfully 'export a runnable jar' using Eclipse. That allowed me to distribute my project as a self contained '.jar' and run it on other systems with a simple: java -jar project.jar

I am using Eclipse 4.3.0.

I decided I wanted to covert the project to a 'maven project' and did so using eclipse 'project/configure/convert to maven'.

I then found that my old mechanism for creating a .jar no longer worked.

Casting about the internet I finally found the information I needed. The first bit of information was that I needed to create what was called a 'Fat Jar'. When using maven, one creates a 'Fat Jar' by running maven, NOT using the export function in Eclipse. To create a 'Fat Jar', one needs to click on 'Project/Run As' and select 'maven build'. That will get you to a screen where you specify 'Goals'. Ultimately, for me, the goals were "compile assembly:single".

Doing this for the first time will result in warnings and errors. The most important error is:

Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.2-beta-5:single

This is because the 'pom.xml' file created by the 'convert' process is incomplete. After several frustrating hours, here is what I finally made work. Again, to create the 'Fat Jar', I do:

project/Run As/Maven build

with goals: compile assembly:single

My 'pom.xml' is:

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>example</groupId>
  <artifactId>example</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>HelloWorld</name>
  <description>Hello World Example</description>
  <packaging>jar</packaging>
    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <release>17</release>
        </configuration>
      </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.3.0</version>
            <configuration>
               <archive>
                  <manifest>
                     <mainClass>example.HelloWorld</mainClass>
                  </manifest>
               </archive>
               <finalName>helloWorld</finalName>
               <descriptorRefs>
                 <descriptorRef>jar-with-dependencies</descriptorRef>
               </descriptorRefs>
            </configuration>
       </plugin>
    </plugins>
  </build>
</project>

Final notes:

The section concerning 'UTF-8' is not strictly needed, it just gets rid of an annoying warning.

There may be other 'unneeded' lines in the 'pom.xml'.

The 'compile' may be unnecessary but I found that when editing the pom.xml file I often got rid of Eclipse syntax type errors by running 'compile' and so I just do it as a matter of course now.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ae6ty