Search code examples
javaspring-cloud-task

java.lang.ClassNotFoundException on a Spring Cloud Task


I'm trying out a simple Spring Cloud Task application. I'm currently trying to run the said application but somehow I'm ending up with a java.lang.ClassNotFoundException: com.example.sampletask.demotask.DemoTaskApplication.java. I'm trying to tweak the pom.xml file but I'm getting no where so far. Here's what I have as of now:

The application class:

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!");    
        }
    }

}

Here's my 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>

And I thought I should add the application.properties as well:

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

What I tried so far is changing the value of the <start-class> tag couple of times but all it does is update the package mentioned in the error. Any help will be much appreciated.


Solution

  • I think you need to drop the .java from this line in your pom.xml:

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

    I suggest you go to start.spring.io to generate a project with the latest versions of spring-boot and spring-cloud-task though as the versions you have in your pom.xml are quite old now.