繁体   English   中英

Spring Boot 模块中的“错误:无法找到或加载主类”

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

我有一个位于项目模块中的Spring Boot应用程序。

模块有自己的 pom.xml,外部项目有自己的pom.xml

模块的pom.xml具有指向应用程序主类的<mainClass>...</mainClass>注释。

它还具有对具有<parent>...</parent>属性的父pom.xml的引用,指向父artifactId, groupId & version

在 Intellij 中运行应用程序时,它运行正常。

问题是尝试从 jar 运行应用程序时:

 java -jar myApp.jar

然后我们得到和错误

错误:无法找到或加载主类 com.example.server.Application

我们在这里缺少什么?

编辑 - 添加 POM 文件:

父 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">
<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>

模块 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">
<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>

如果使用的是Linux,请使用winzip或winrar打开jar文件,请使用存档管理器。 然后转到META-INF,打开MANIFEST.MF并确保属性“ Main-Class:your.class.path”正确。

我遇到了同样的错误,但是在一个带有 javafx 的简单 java 项目中,我已经构建了该项目,出于某种原因,我想更改它找到主类的包的名称,当我运行程序时,我得到了那个错误。 我解决它:

  1. 修改引用主类的文件“module-info.java”

  2. 我还修改了 pom.xml

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

在那里你必须参考包含主类的包

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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