Java Spring 应用程序启动错误:源不能为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34905675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:06:46  来源:igfitidea点击:

Spring application start error: sources must not be empty

javaspringrest

提问by Adipati Karna

I'm following a Spring tutorial, and when I tried to start the spring application I got the following error:

我正在学习 Spring 教程,当我尝试启动 Spring 应用程序时,出现以下错误:

2016-01-20 23:18:15.907 INFO 5271 --- [ main] o.s.boot.SpringApplication : Starting SpringApplication v1.3.1.RELEASE on ...

2016-01-20 23:18:15.911 INFO 5271 --- [ main] o.s.boot.SpringApplication : No active profile set, falling back to default profiles: default

2016-01-20 23:18:15.918 ERROR 5271 --- [ main] o.s.boot.SpringApplication : Application startup failed java.lang.IllegalArgumentException: Sources must not be empty at org.springframework.util.Assert.notEmpty(Assert.java:276) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:352) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:305) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1124) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] at org.springframework.boot.SpringApplication.main(SpringApplication.java:1140) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]

2016-01-20 23:18:15.907 INFO 5271 --- [main] osboot.SpringApplication :启动 SpringApplication v1.3.1.RELEASE on ...

2016-01-20 23:18:15.911 INFO 5271 --- [main] osboot.SpringApplication:没有设置活动配置文件,回退到默认配置文件:默认

2016-01-20 23:18:15.918 错误 5271 --- [main] osboot.SpringApplication:应用程序启动失败 java.lang.IllegalArgumentException:org.springframework.util.Assert.notEmpty(Assert.java) 处的源不能为空:276) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE] 在 org.springframework.boot.SpringApplication.doRun(SpringApplication.java:352) [spring-boot-1.3.1.RELEASE] .jar:1.3.1.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:305) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] 在 org.springframework。 boot.SpringApplication.run(SpringApplication.java:1124) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] 在 org.springframework.boot.SpringApplication.main(SpringApplication.java:1140) [spring -boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]

What is this "Sources must not be empty" error?

这个“来源不能为空”错误是什么?

I am using an Eclipse - Maven project for this tutorial project and I have updated the project. I have also cleaned and rebuilt but I still got this error.

我正在为本教程项目使用 Eclipse - Maven 项目,并且我已经更新了该项目。我也已经清理和重建,但我仍然遇到这个错误。

回答by Adipati Karna

It turns out that I did not set the main class correctly in Eclipse - Debug, and Run configuration.

事实证明,我没有在 Eclipse - 调试和运行配置中正确设置主类。

I set org.springframework.boot.SpringApplicationas the main class.

我将org.springframework.boot.SpringApplication设置为主类。

The Main class should be pointing to my main class.

Main 类应该指向我的主类。

回答by Maksim Kostromin

you must add at least one main config class as a source (see attached screenshots).

您必须至少添加一个主配置类作为源(请参阅附加的屏幕截图)。

wrong: enter image description here

错误的: 在此处输入图片说明

right: enter image description here

对: 在此处输入图片说明

回答by user10370099

add scanBasePackages="com.login" at main class thats all your code will work.

在主类中添加 scanBasePackages="com.login" ,您的所有代码都将起作用。

package com.login.example.LoginExample;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages="com.login")

public class LoginExampleApplication {

public static void main(String[] args) {
    SpringApplication.run(LoginExampleApplication.class, args);
    }
}

回答by Marcel

Had the same issue recently, turned out I was running the wrong java class. I went to class where I have my @SpringBootApplication and right click -> run as java application. Maybe that will help someone as silly as me.

最近遇到了同样的问题,结果我运行了错误的 Java 类。我去了有我的@SpringBootApplication 的课堂,然后右键单击 -> 作为 Java 应用程序运行。也许这会帮助像我这样愚蠢的人。