繁体   English   中英

如何解决在 spring 引导时找不到或加载主 class 错误?

[英]How to solve could not find or load main class error with spring boot?

我为 Spring 引导创建了一个 Maven 项目。 我有很多 Spring 依赖项和一个主要的 class:

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 文件中。 Even though I triple checked that the directory structure and main class file appear correctly in the pom.xml I keep getting the error: Error: Could not find or load main class com.vastserver.MyArtifactApplication when I run mvn package and then java -cp target/vast-ad-server-artifactId-1.0-SNAPSHOT-jar-with-dependencies.jar com.vastserver.MyArtifactApplicationmvn exec:exec 如果我从 Intellij 运行主要的 class 确实可以工作,所以我知道代码不是问题,而是 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>

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

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

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

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

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

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

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

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

添加以上所有答案,右键单击project-> Maven ->update project-> force update

这将下载所有缺少的依赖项。

还是不行的话?? 右键单击pom.xml并作为配置运行。 检查您使用的是正确的 JRE。 然后将目标设置为安装并运行它。

重要的是检查 pom.xml 中的 java 版本,如果它与您安装的 JRE 版本不匹配,那么您也可能会收到此错误。 所以确保它是一样的。 还是不行?? 从工作区中删除项目并将其重新导入

右键单击project->Maven->UpdateProject->勾选Force update of Snapshots/Releases-OK

给时间更新项目。 然后只需运行应用程序 Rightclickonproject->Runas->Spring Boot App

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

---右键项目---转到maven---更新项目

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

  1. Go 运行 --> 运行配置 - 类路径,在用户条目中添加您的项目。
  2. Go 到运行配置的源选项卡并添加工作区文件夹和 select 您的 src 文件,其中您的主要方法 class 在那里。 单击应用并执行。 它会起作用的。

这可能是由 JRE 路径引起的。 更改 JRE 路径并清理项目,它将起作用。 Java-Spring-boot-sts 类路径问题

sts类路径问题

暂无
暂无

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

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