首页 > 解决方案 > Spring Cloud 任务上的 java.lang.ClassNotFoundException

问题描述

我正在尝试一个简单的 Spring Cloud Task 应用程序。我目前正在尝试运行上述应用程序,但不知何故我最终得到了一个java.lang.ClassNotFoundException: com.example.sampletask.demotask.DemoTaskApplication.java. 我正在尝试调整pom.xml文件,但到目前为止我没有得到任何地方。这是我现在所拥有的:

应用类:

package com.example.sampletask.demotask;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.task.configuration.EnableTask;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EnableTask
public class DemoTaskApplication {

    @Bean
    public CommandLineRunner commandLineRunner() {
        return new HelloWorldCommandLineRunner();
    }
    
    public static void main(String[] args) {
        SpringApplication.run(DemoTaskApplication.class, args);
    }
    
    public static class HelloWorldCommandLineRunner implements CommandLineRunner {

        public void run(String... arg0) throws Exception {
            System.out.println("Hello, World!");    
        }
    }

}

这是我的pom:

<?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 https://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>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example.sampletask</groupId>
    <artifactId>demo-task</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-task</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <!-- <java.version>11</java.version> -->
        <start-class>com.example.sampletask.demotask.DemoTaskApplication.java</start-class>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-task-core</artifactId>
            <version>1.2.2.RELEASE</version>
        </dependency>
    </dependencies>

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

</project>

我想我也应该添加 application.properties :

logging.level.org.springframework.cloud.task=DEBUG
spring.application.name=helloWorld

到目前为止,我尝试的是<start-class>多次更改标签的值,但它所做的只是更新错误中提到的包。任何帮助都感激不尽。

标签: javaspring-cloud-task

解决方案


我认为您需要.java从 pom.xml 中的这一行中删除:

<start-class>com.example.sampletask.demotask.DemoTaskApplication.java</start-class>

我建议你去start.spring.io使用最新版本的 spring-boot 和 spring-cloud-task 生成一个项目,尽管你在 pom.xml 中的版本现在已经很旧了。


推荐阅读