为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕妹3146593 2023-03-23 15:45:23
我为 Spring Boot 创建了一个 Maven 项目。我有很多 Spring 依赖项和一个主类:package com.vastserver;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic 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 构建设置。我迷失了我的问题所在。
查看完整描述

6 回答

?
拉莫斯之舞

TA贡献1820条经验 获得超10个赞

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



查看完整回答
反对 回复 2023-03-23
?
一只甜甜圈

TA贡献1836条经验 获得超5个赞

我意识到spring-boot-maven-plugin实际上是在构建,所以不需要其他插件。如果将 maven 中的插件部分编辑为:


<plugins>

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

            </plugin>

</plugins>

然后运行mvn package并java -jar target/vast-ad-server-artifactId-1.0-SNAPSHOT.jar工作。


查看完整回答
反对 回复 2023-03-23
?
繁星淼淼

TA贡献1775条经验 获得超11个赞

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

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

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


查看完整回答
反对 回复 2023-03-23
?
PIPIONE

TA贡献1829条经验 获得超9个赞

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



查看完整回答
反对 回复 2023-03-23
?
一只萌萌小番薯

TA贡献1795条经验 获得超7个赞

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

---右键项目---go to maven ---更新项目


查看完整回答
反对 回复 2023-03-23
?
皈依舞

TA贡献1851条经验 获得超3个赞

类路径中缺少您的源文件。对我来说,它发生在我重新启动 STS 时。

  1. 转到 Run --> Run Configurations- classpath,在用户条目中添加您的项目。

  2. 转到运行配置的源选项卡并添加工作区文件夹,然后选择主方法类所在的 src 文件。单击应用并执行。它会起作用。


查看完整回答
反对 回复 2023-03-23
  • 6 回答
  • 0 关注
  • 141 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信