如何解决 spring boot 找不到或加载主类错误?

新手上路,请多包涵

我为 Spring Boot 创建了一个 Maven 项目。我有很多 Spring 依赖项和一个主类:

 package com.vastserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyArtifactApplication {

    public static void main(String[] args) {

//      SpringApplication.run(MyArtifactApplication.class, args);
        System.out.println("hello!");
    }

}

src 目录的文件夹结构为:

 .
└── main
    ├── java
    │   └── com
    │       └── vastserver
    │           └── MyArtifactApplication.java
    └── resources
        └── application.properties

在我的 pom.xml 中,我使用 maven-assembly-plugin 以便在独立的 .jar 文件中构建我的项目。即使我三重检查目录结构和主类文件是否正确显示在 pom.xml 中,我仍然收到错误: Error: Could not find or load main class com.vastserver.MyArtifactApplication 当我运行 mvn package 然后 java -cp target/vast-ad-server-artifactId-1.0-SNAPSHOT-jar-with-dependencies.jar com.vastserver.MyArtifactApplicationmvn exec:exec 。如果我从 Intellij 运行它,主类确实有效,所以我知道代码不是问题,而是 Maven 构建设置。我迷失了我的问题所在。

我的 pom.xml 如下所示:

 <?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>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <maven.compiler.release>11</maven.compiler.release>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <mainClass>com.vastserver.MyArtifactApplication</mainClass>
        <descriptorRef>jar-with-dependencies</descriptorRef>
        <targetSnapshot>target/vast-ad-server-artifactId-1.0-SNAPSHOT</targetSnapshot>
        <targetWithDependencies>${targetSnapshot}-${descriptorRef}.jar</targetWithDependencies>
    </properties>

    <groupId>com.vastserver</groupId>
    <artifactId>vast-ad-server-artifactId</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>${mainClass}</mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>${descriptorRef}</descriptorRef>
                            </descriptorRefs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.3.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <executable>java</executable>
                    <arguments>
                        <argument>-cp</argument>
                        <argument>${targetWithDependencies}</argument>
                        <argument>${mainClass}</argument>
                    </arguments>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

原文由 hitchhiker 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.3k
2 个回答

我认为您应该检查由 maven 构建的工件的目录结构。通常,spring boot 工件是由一个特殊的 spring boot 插件准备的,而不是由 maven 程序集插件准备的。

虽然它共享“jar”后缀,但它并不是真正的 jar,它有特殊的类加载器从 BOOT-INF/lib 文件夹加载类。

我已经在 此处 提供了有关 spring boot 应用程序启动时究竟发生了什么的详细答案,但底线是,如果您使用程序集插件,则必须同时准备清单文件和相当复杂的文件夹结构。坦率地说,我认为你应该使用 spring boot 插件作为构建 spring boot 应用程序的首选。

原文由 Mark Bramnik 发布,翻译遵循 CC BY-SA 4.0 许可协议

对我来说:Maven - 更新项目有效。

原文由 neves 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题