如何解决 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!");

    }


}

directory的文件夹结构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.MyArtifactApplicationor mvn exec:exec。如果我从 Intellij 运行它,主类确实有效,所以我知道代码不是问题,而是 Maven 构建设置。我迷失了我的问题所在。


慕妹3146593
浏览 145回答 6
6回答

拉莫斯之舞

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

一只甜甜圈

我意识到spring-boot-maven-plugin实际上是在构建,所以不需要其他插件。如果将 maven 中的插件部分编辑为:<plugins>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-maven-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </plugin></plugins>然后运行mvn package并java -jar target/vast-ad-server-artifactId-1.0-SNAPSHOT.jar工作。

繁星淼淼

我认为您应该检查由 maven 构建的工件的目录结构。通常,spring boot 工件是由一个特殊的 spring boot 插件准备的,而不是由 maven 程序集插件准备的。虽然它共享“jar”后缀,但它并不是真正的 jar,它有特殊的类加载器从文件夹加载类BOOT-INF/lib。我已经在此处提供了有关 spring boot 应用程序启动时究竟发生了什么的详细答案,但底线是,如果您使用程序集插件,则必须同时准备清单文件和相当复杂的文件夹结构。坦率地说,我认为你应该使用 spring boot 插件作为构建 spring boot 应用程序的首选。

PIPIONE

maven --> 更新它对我有用的项目

一只萌萌小番薯

尝试在 Maven 中更新项目。有时在添加新的依赖项时它希望在那里更新 Maven 开发工具将不起作用---右键项目---go to maven ---更新项目

皈依舞

类路径中缺少您的源文件。对我来说,它发生在我重新启动 STS 时。转到 Run --> Run Configurations- classpath,在用户条目中添加您的项目。转到运行配置的源选项卡并添加工作区文件夹,然后选择主方法类所在的 src 文件。单击应用并执行。它会起作用。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java