简体   繁体   English

无法在 Eclipse 中运行 Spring Boot 项目

[英]Can not run Spring Boot project in eclipse

I am trying to learn eclipse for a Java project.我正在尝试为 Java 项目学习 Eclipse。 I used initializer( https://start.spring.io/ ) to set up Spring Boot using a dependency of spring web.我使用初始化程序( https://start.spring.io/ )使用 spring web 的依赖项来设置 Spring Boot。 I try to build my project in eclipse and I get the following error我尝试在 Eclipse 中构建我的项目,但出现以下错误

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.5.2)


2021-07-05 12:20:50.248  INFO 23536 --- [           main] o.s.boot.SpringApplication               : Starting SpringApplication v2.5.2 using Java 11.0.2 on MSI with PID 23536 (C:\Users\user\.m2\repository\org\springframework\boot\spring-boot\2.5.2\spring-boot-2.5.2.jar started by user in C:\Users\user\Downloads\app)
2021-07-05 12:20:50.252  INFO 23536 --- [           main] o.s.boot.SpringApplication               : No active profile set, falling back to default profiles: default
2021-07-05 12:20:50.445 ERROR 23536 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalArgumentException: Sources must not be empty
    at org.springframework.util.Assert.notEmpty(Assert.java:470) ~[spring-core-5.3.8.jar:5.3.8]
    at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:425) ~[spring-boot-2.5.2.jar:2.5.2]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:337) ~[spring-boot-2.5.2.jar:2.5.2]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-2.5.2.jar:2.5.2]
    at org.springframework.boot.SpringApplication.main(SpringApplication.java:1359) ~[spring-boot-2.5.2.jar:2.5.2]

Any ideas?有任何想法吗?

Maybe you're running the wrong class.也许你跑错了课程。 Run the class which is annotated with @SpringBootApplication运行带有@SpringBootApplication注释的类

This exact exception and stack trace is thrown in SpringApplication (as of version 2.5.2) when no sources ( Class<?>... ) were provided.当没有提供源( Class<?>... )时, SpringApplication (从版本 2.5.2 开始)会抛出这个确切的异常和堆栈跟踪。 This happens when called directly or with the SpringApplicationBuilder like this:直接调用或使用SpringApplicationBuilder调用时会发生这种情况,如下所示:

public static void main(String[] args) {
    new SpringApplicationBuilder()
            .registerShutdownHook(true)
            .run(args);
}
// or
public static void main(String[] args) {
    new SpringApplication().run(args);
}

To fix this, pass your class to the builder (eg DemoApplication.class ) like this:要解决此问题,请将您的类传递给构建器(例如DemoApplication.class ),如下所示:

public static void main(String[] args) {
    new SpringApplicationBuilder(DemoApplication.class)
            .registerShutdownHook(true)
            .run(args);
}
// or
public static void main(String[] args) {
    new SpringApplication(DemoApplication.class).run(args);
}

Another option would be to run your application with the static helper method like this:另一种选择是使用静态辅助方法运行您的应用程序,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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