-1

I am having an issue with Maven and would need your help to resolve it. I have three Spring Boot projects A, B, and C. In project A, there's a main class annotated with @SpringBootApplication. Project A requires a bean of type X, and both B and C initialize a bean of this type.

For architectural reasons, I need one project that uses A and B, and another project that uses A and C. Therefore, I've set up two Maven projects, each with an empty Java codebase, and a pom that imports:

  1. The dependencies of A and B
  2. The dependencies of A and C
<?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>
    <groupId>group</groupId>
    <artifactId>testAandB</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <java.version>17</java.version>
    </properties>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>group</groupId>
            <artifactId>A</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>group</groupId>
            <artifactId>B</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>group.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

When I run "mvn install," the project compiles successfully. However, when I try to execute it using "java -jar," I encounter the following error: Error: Could not find or load the main class group.Main Caused by: java.lang.ClassNotFoundException: group.Main

Am I doing something wrong? How can I resolve this issue?

Thank you in advance.

SkyBlue
  • 1
  • 1
  • You need to get some maven plugin which will generate fat-jar for you, which contain all needed dependencies. My favorite one is maven-shade-plugin – Morph21 Sep 01 '23 at 07:02
  • Spring Boot generates a special fatjar which has a different structure and thus cannot be used as a dependency. – M. Deinum Sep 01 '23 at 07:06
  • Check out [this](https://stackoverflow.com/a/23986765/2764255) answer. In any case `java -jar` requires the dependencies in the classpath, so it should be something like `java -cp /path/to/A.jar:/path/to/B.jar -jar X.jar`. For a couple of dependencies, this is doable; for more and, even worse, for transitive dependencies, you need some kind of automation. – Nikos Paraskevopoulos Sep 01 '23 at 07:07

0 Answers0