[英]"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.我有一个位于项目模块中的Spring Boot
应用程序。
The module has its own pom.xml and the outer project has its own pom.xml
模块有自己的 pom.xml,外部项目有自己的pom.xml
The module's pom.xml
has the <mainClass>...</mainClass>
annotation pointing to the main class of the app.模块的pom.xml
具有指向应用程序主类的<mainClass>...</mainClass>
注释。
It also has a reference to the parent pom.xml
with the <parent>...</parent>
property, pointing to the parent artifactId, groupId & version
.它还具有对具有<parent>...</parent>
属性的父pom.xml
的引用,指向父artifactId, groupId & version
。
When running the app within Intellij, it runs OK.在 Intellij 中运行应用程序时,它运行正常。
The problem is when trying to run the app from the jar:问题是尝试从 jar 运行应用程序时:
java -jar myApp.jar
Then we get and error of然后我们得到和错误
Error: Could not find or load main class com.example.server.Application错误:无法找到或加载主类 com.example.server.Application
What are we missing here?我们在这里缺少什么?
EDIT - Adding the POM files:编辑 - 添加 POM 文件:
The parent pom file:父 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>
The module pom file:模块 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>
Open your jar file with winzip or winrar, if you are using linux use archive manager. 如果使用的是Linux,请使用winzip或winrar打开jar文件,请使用存档管理器。 Then go to the META-INF, open the MANIFEST.MF and make sure that the property "Main-Class: your.class.path" is correct. 然后转到META-INF,打开MANIFEST.MF并确保属性“ Main-Class:your.class.path”正确。
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.我遇到了同样的错误,但是在一个带有 javafx 的简单 java 项目中,我已经构建了该项目,出于某种原因,我想更改它找到主类的包的名称,当我运行程序时,我得到了那个错误。 I solve it:我解决它:
Modifying the file "module-info.java" where the main class is referred to修改引用主类的文件“module-info.java”
I also modified the pom.xml我还修改了 pom.xml
<configuration><mainClass>pathToTheMainClass</mainClass></configuration>
There you have to refer to the package who contains main class在那里你必须参考包含主类的包
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.