【发布时间】:2019-04-28 14:39:27
【问题描述】:
我的 Spring Boot/Mavan 应用程序中出现此错误。这个应用程序不能从带有参数的命令行运行(参见下面的“hello”示例):
$ mvn clean package
...
$ java -jar target/dataloads-0.0.1-SNAPSHOT.jar hello
Error: Could not find or load main class com.example.dataloads.SpringBootConsoleApplication
下面是我的 SpringBootConsoleApplication 类:
package com.example.dataloads;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootConsoleApplication implements CommandLineRunner {
public static void main(String[] args) {
System.out.println("STARTING THE APPLICATION");
SpringApplication.run(SpringBootConsoleApplication.class, args);
System.out.println("APPLICATION FINISHED");
}
public void run(String... args) {
System.out.println("EXECUTING : command line runner");
for (int i = 0; i < args.length; ++i) {
System.out.println(args[i]);
}
}
}
下面是我正在使用的 pom.xml 文件,注意我在哪里声明了 main 函数:
<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>
<groupId>com.example</groupId>
<artifactId>dataloads</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>dataloads</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.example.dataloads.SpringBootConsoleApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
我错过了什么吗?如果有帮助,我一直在关注本教程 - https://www.baeldung.com/spring-boot-console-app - 但是,由于收到警告/错误(例如,在 pom.xml 中声明 main 方法),我不得不进行一些小的修改。
【问题讨论】:
-
您的课程是否真的位于文件夹 com/example/dataloads 中(区分大小写)?你可以检查吗 ?我最近遇到了一些错误定位的类,但编译器没有任何警告(!)
-
右键项目>maven>更新项目
-
你能不能用 zip 文件阅读器(如 7zip)打开 jar 文件并检查它是否包含文件夹层次结构和 .class 文件?
标签: java spring maven spring-boot