mavenparent-pom

Maven modules - java.lang.NoClassDefFoundError


I can't run this... Anyone what could I be doing wrong?

modules
 | server
 |   |_com
 |    |_test
 |     |_app
 |      |_Main.java
 | |_pom.xml (server_pom)
 | comum
 |   |_com
 |    |_test
 |     |_app
 |      |_interfaces
 |       |_ServerInterface.java
 | |_pom.xml (comum_pom)
 | client
 |   |_com
 |    |_test
 |     |_app
 |      |_Main.java
 | |_pom.xml (client_pom)
 |_pom.xml (modules_pom)

modules_pom

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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>com.test.app</groupId>
    <artifactId>modules</artifactId>
    <version>1.0-SNAPSHOT</version>    
    <packaging>pom</packaging>            
    
    <modules>
        <module>comum</module>
        <module>server</module>
        <module>client</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
</project>

server_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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
    <groupId>com.test.app.server</groupId>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>server</artifactId>

    <!-- Parent POM -->
    <parent>
        <artifactId>modules</artifactId>
        <groupId>com.test.app</groupId>        
        <version>1.0-SNAPSHOT</version>
    </parent> 
       
    <!-- Comun / Interfaces -->
    <dependencies>
        <dependency>
            <groupId>com.test.app.comum</groupId>
            <artifactId>comum</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>            

</project>

comum_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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
    <groupId>com.test.app.comum</groupId>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>comum</artifactId>

    <!-- Parent POM -->
    <parent>
        <artifactId>modules</artifactId>
        <groupId>com.test.app</groupId>        
        <version>1.0-SNAPSHOT</version>
    </parent>    
                    
</project>

client_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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
    <groupId>com.test.app.client</groupId>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>client</artifactId>

    <!-- Parent POM -->
    <parent>
        <artifactId>modules</artifactId>
        <groupId>com.test.app</groupId>        
        <version>1.0-SNAPSHOT</version>
    </parent> 
       
    <!-- Comun / Interfaces -->
    <dependencies>
        <dependency>
            <groupId>com.test.app.comum</groupId>
            <artifactId>comum</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>            

</project>

When I run the mvn package:

mvn package

.
.
.
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] modules                                                            [pom]
[INFO] comum                                                              [jar]
[INFO] server                                                             [jar]
[INFO] client                                                             [jar]
[INFO] 
[INFO] ---------------< com.test.app:modules >----------------
.
.
.
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for modules 1.0-SNAPSHOT:
[INFO] 
[INFO] modules ............................................ SUCCESS [  0.014 s]
[INFO] comum .............................................. SUCCESS [  2.084 s]
[INFO] server ............................................. SUCCESS [  0.098 s]
[INFO] client ............................................. SUCCESS [  0.114 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

But...

java -cp server/target/server-1.0-SNAPSHOT.jar com.test.app.server.Main 

Exception in thread "main" java.lang.NoClassDefFoundError: com/test/app/comum/interfaces/ServerInterface

Tks


Solution

  • Probably the last version of the dependency (comum) that is on the "server" module, was not installed in the m2 directory.

    Run mvn install on "comum" module and be sure to the existed jar file that is on m2 folder has the compiled "_ServerInterface".