简体   繁体   English

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

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

I created a Maven project for Spring Boot.我为 Spring 引导创建了一个 Maven 项目。 I have a lot of Spring dependencies and one main class:我有很多 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!");
    }

}

The folder structure of src directory is: src目录的文件夹结构为:

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

In my pom.xml I use maven-assembly-plugin in order to build my project in a standalone.jar file.在我的 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.MyArtifactApplication or mvn exec:exec . 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 The main class does work if I run it from Intellij so I know the code is not the problem but rather Maven build settings.如果我从 Intellij 运行主要的 class 确实可以工作,所以我知道代码不是问题,而是 Maven 构建设置。 I lost at where my problem could be.我迷失了我的问题所在。

My pom.xml looks as follows:我的 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>

I realized that the spring-boot-maven-plugin actually does the building, so other plugins are not needed.我意识到spring-boot-maven-plugin实际上做了构建,所以不需要其他插件。 If the plugins section in maven is edited to:如果 maven 中的 plugins 部分被编辑为:

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

Then running mvn package and java -jar target/vast-ad-server-artifactId-1.0-SNAPSHOT.jar works.然后运行mvn packagejava -jar target/vast-ad-server-artifactId-1.0-SNAPSHOT.jar工作。

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

I think you should check the directory structure of the artifact that was built by maven.我认为您应该检查由 maven 构建的工件的目录结构。 Usually, spring boot artifacts are prepared by a special spring boot plugin and not by a maven assembly plugin.通常,spring boot artifact 是由特殊的 spring boot 插件准备的,而不是由 maven assembly 插件准备的。

Although it shares the "jar" suffix, it's not really a jar, it has special classloader to load classes from BOOT-INF/lib folder.尽管它共享“jar”后缀,但它并不是真正的 jar,它具有特殊的类加载器来从BOOT-INF/lib文件夹加载类。

I've already provided a detailed answer on what happens exactly when the spring boot application starts here but bottom line if you use assembly plugin you'll have to prepare both manifest file and a fairly complicated structure of folders.我已经提供了有关 Spring Boot 应用程序在此处启动时究竟会发生什么的详细答案,但最重要的是,如果您使用程序集插件,则必须准备清单文件和相当复杂的文件夹结构。 Frankly, I think you should use spring boot plugin as a first resort to build spring boot applications.坦率地说,我认为您应该使用 spring boot 插件作为构建 spring boot 应用程序的首选。

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

Adding to all the above answers right click on project-> Maven ->update project-> force update添加以上所有答案,右键单击project-> Maven ->update project-> force update

This will download all missing dependencies.这将下载所有缺少的依赖项。

Still if it doesn't work ??还是不行的话?? right click on pom.xml and run as configuration.右键单击pom.xml并作为配置运行。 Check you are using correct JRE.检查您使用的是正确的 JRE。 Then set goal as install and run it.然后将目标设置为安装并运行它。

Important check java version in pom.xml if it doesn't match with your installed JRE version then also you might get this error.重要的是检查 pom.xml 中的 java 版本,如果它与您安装的 JRE 版本不匹配,那么您也可能会收到此错误。 So make sure its same.所以确保它是一样的。 Still doesn't work??还是不行?? Delete the project from workspace and import it back .从工作区中删除项目并将其重新导入

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

give time to update the project.给时间更新项目。 Then simply run the application Rightclickonproject->Runas->Spring Boot App然后只需运行应用程序 Rightclickonproject->Runas->Spring Boot App

try updating project in maven.尝试在 Maven 中更新项目。 Sometimes while adding new dependency it wants maven to be updated there Dev tools won't work有时,在添加新的依赖项时,它希望 Maven 得到更新,那里开发工具将不起作用

---Right click project ---go to maven ---Update project ---右键项目---转到maven---更新项目

Your source file is missing from classpath.类路径中缺少您的源文件。 For me it happened when I restarted STS.对我来说,它发生在我重新启动 STS 时。

  1. Go to Run -->Run Configurations- classpath, in user entries add your project. Go 运行 --> 运行配置 - 类路径,在用户条目中添加您的项目。
  2. Go to source tab of Run configurations and add workspace folder and select your src file where your main method class is there. Go 到运行配置的源选项卡并添加工作区文件夹和 select 您的 src 文件,其中您的主要方法 class 在那里。 Click on apply and execute.单击应用并执行。 It will work.它会起作用的。

It may be caused by JRE path.这可能是由 JRE 路径引起的。 Change JRE path and clean project, it will work.更改 JRE 路径并清理项目,它将起作用。 Java-Spring-boot-sts 类路径问题

sts类路径问题

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

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