简体   繁体   中英

Spring Boot/ Maven command line app: Could not find or load main class

I'm getting this error within my Spring Boot/Mavan app. This app is intended t be run from the command line with arguments (see "hello" example below):

$ 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

Below is my SpringBootConsoleApplication class:

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]);
        }
    }
}

Below is the pom.xml file I'm using, notice where I've declared the main function:

<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>

Am I missing anything? I've been following this tutorial if it helps - https://www.baeldung.com/spring-boot-console-app - however, had to make some small amendments as I was getting warnings/errors (eg declare main method in pom.xml).

Have you tried using "spring-boot-maven-plugin" instead of "maven-jar-plugin" ?

See an example here: https://www.mkyong.com/spring-boot/spring-boot-non-web-application-example/

I just tried and worked like a charm, without any modifications :)

Hope it helps, cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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