Search code examples
javamavenjarmaven-assembly-pluginmulti-module

Maven assembly jar execution Could not find or load main class Caused by: java.lang.ClassNotFoundException


I have been trying, for several days now, to create an executable jar file for my muli-module maven project, sadly I have had no luck on doing it.

I know that there are a lot of similar questions already, but, even by following the answers, i cannot manage to run the jar I make.

I am using the maven-assembly plugin, so that my jar contains all the required dependencies, here is the pom:

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <artifactId>tam-authentication</artifactId>
    <name>tam-authentication</name>
    <description>authentication tam project</description>

    <parent>
        <groupId>com.netcomgroup.eu</groupId>
        <artifactId>TAM</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <properties>
        <java.version>11</java.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
            </plugin>
        </plugins>

        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <archive>
                            <manifest>
                                <mainClass>"fully qualified class name"</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly</id>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <dependencies>
        ...
        dependencies from other modules
        ...
    </dependencies>

</project>

futhermore i have another probably related problem regarding jar creation with a common library included:

using Eclipse as IDE, whenever I run as > maven install on the multimodule project, I often get jars failing over correct imports, that I need to delete and import again to complete the java building process correctly. Sometimes i simply must run maven install several times in a row to make the jar building process succeed.

I don't know if the second problem is related but i guess there is some mistake I cannot see in the multi module project jar building.


Solution

  • First of all, as per your pom.xml you did not provide the fully qualified Main class name in your dependency. Second if you provided the Main class name, then after "mvn clean install", it would create 2 jars - one with artifact name and one with "jar-with-dependencies" and you have to user "jar-with-dependencies". Also i don't think you need to use pluginManagement tag as an extra step. I have just modified a bit your pom below, please try this and check.

    <?xml version="1.0" encoding="UTF-8"?>
    

    4.0.0

    <artifactId>tam-authentication</artifactId>
    <name>tam-authentication</name>
    <description>authentication tam project</description>
    
    <parent>
        <groupId>com.netcomgroup.eu</groupId>
        <artifactId>TAM</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    
    <properties>
        <java.version>11</java.version>
    </properties>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                  <execution>
                    <phase>package</phase>
                    <goals>
                      <goal>single</goal>
                    </goals>
                  </execution>
                </executions>
                <configuration>
                  <archive>
                    <manifest>
                      <addClasspath>true</addClasspath>
                      <mainClass>{Your fully qualified Main class eg - abc.cde.Main}</mainClass>
                    </manifest>
                  </archive>
                  <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                  </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    
    </build>
    
    <dependencies>
        ...
        dependencies from other modules
        ...
    </dependencies>