java.lang.ClassNotFoundException在Spring Cloud Task中发生的错误。

huangapple go评论73阅读模式
英文:

java.lang.ClassNotFoundException on a Spring Cloud Task

问题

我正在尝试一个简单的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 文件:

<?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 标签的值,但它只是更新了错误中提到的包名。非常感谢您的任何帮助。

英文:

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(&quot;Hello, World!&quot;);	
		}
	}

}

Here's my pom:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
	xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
	&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
	&lt;parent&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
		&lt;version&gt;1.5.2.RELEASE&lt;/version&gt;
		&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
	&lt;/parent&gt;
	&lt;groupId&gt;com.example.sampletask&lt;/groupId&gt;
	&lt;artifactId&gt;demo-task&lt;/artifactId&gt;
	&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
	&lt;name&gt;demo-task&lt;/name&gt;
	&lt;description&gt;Demo project for Spring Boot&lt;/description&gt;

	&lt;properties&gt;
		&lt;!-- &lt;java.version&gt;11&lt;/java.version&gt; --&gt;
		&lt;start-class&gt;com.example.sampletask.demotask.DemoTaskApplication.java&lt;/start-class&gt;
	&lt;/properties&gt;

	&lt;dependencies&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;
		&lt;/dependency&gt;
		
		&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
			&lt;artifactId&gt;spring-cloud-task-core&lt;/artifactId&gt;
			&lt;version&gt;1.2.2.RELEASE&lt;/version&gt;
		&lt;/dependency&gt;
	&lt;/dependencies&gt;

	&lt;build&gt;
		&lt;plugins&gt;
			&lt;plugin&gt;
				&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
				&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
			&lt;/plugin&gt;
		&lt;/plugins&gt;
	&lt;/build&gt;

&lt;/project&gt;

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 &lt;start-class&gt; tag couple of times but all it does is update the package mentioned in the error. Any help will be much appreciated.

答案1

得分: 1

我认为你需要在你的 pom.xml 文件中的这一行中去掉 .java

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

我建议你访问 start.spring.io,使用最新版本的 spring-boot 和 spring-cloud-task 生成一个项目,因为你在 pom.xml 中使用的版本现在相当旧了。

英文:

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

&lt;start-class&gt;com.example.sampletask.demotask.DemoTaskApplication.java&lt;/start-class&gt;

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.

huangapple
  • 本文由 发表于 2020年9月23日 16:54:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64024347.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定