简体   繁体   中英

"Error: Could not find or load main class" in a Module of Spring Boot

I have a Spring Boot app that is in a Module within a project.

The module has its own pom.xml and the outer project has its own pom.xml

The module's pom.xml has the <mainClass>...</mainClass> annotation pointing to the main class of the app.

It also has a reference to the parent pom.xml with the <parent>...</parent> property, pointing to the parent artifactId, groupId & version .

When running the app within Intellij, it runs OK.

The problem is when trying to run the app from the jar:

 java -jar myApp.jar

Then we get and error of

Error: Could not find or load main class com.example.server.Application

What are we missing here?

EDIT - Adding the POM files:

The parent pom file:

<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-app</artifactId>
<packaging>pom</packaging>
<version>5.1-SNAPSHOT</version>
<modules>
    <module>my-app-server</module>
</modules>

<parent>
    <groupId>com.example</groupId>
    <artifactId>my-parent</artifactId>
    <version>LATEST</version>
</parent>

<repositories>
 ...
</repositories>


<name>...</name>
<description>...</description>


<properties>
 ...
</properties>

The module pom file:

<?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">
<parent>
    <artifactId>my-app</artifactId>
    <groupId>com.example</groupId>
    <version>5.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<groupId>com.example.my-app</groupId>
<artifactId>my-app-server</artifactId>

<repositories>
  ...
</repositories>


<dependencies>
...

</dependencies>

<build>
    <plugins>


        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <optimize>true</optimize>
                <debug>true</debug>
                <forceJavacCompilerUse>true</forceJavacCompilerUse>
                <showWarnings>true</showWarnings>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <createDependencyReducedPom>true</createDependencyReducedPom>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.example.server.Application</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring-boot-version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <id>assembly</id>
                    <phase>package</phase>

                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <tarLongFileMode>posix</tarLongFileMode>
                        <descriptors>
                            <descriptor>package.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

Open your jar file with winzip or winrar, if you are using linux use archive manager. Then go to the META-INF, open the MANIFEST.MF and make sure that the property "Main-Class: your.class.path" is correct.

I had the same error but in a simple java project with javafx, I had already built the project and for some reason I wanted to change the name of the package where it found the main class, when I ran the program I got that error. I solve it:

  1. Modifying the file "module-info.java" where the main class is referred to

  2. I also modified the pom.xml

     <configuration><mainClass>pathToTheMainClass</mainClass></configuration>

There you have to refer to the package who contains main class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM