【问题标题】:Spring Boot Samples FileNot found errorSpring Boot 示例文件未找到错误
【发布时间】:2026-02-16 13:20:03
【问题描述】:

我正在关注有关 Spring Boot 项目的示例。 我已经通过 maven 编译并创建了一个 jar 文件,但在尝试运行时出错。

https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-simple

项目文件夹:

错误:

java -jar spring-boot-sample-simple-1.1.3.BUILD-SNAPSHOT.jar

014-06-25 09:10:00.653 ERROR 12028 --- [           main] o.s.boot.SpringApplication               : Application startup failed

ava.lang.IllegalStateException: Unable to load configuration files
       at org.springframework.boot.context.config.ConfigFileApplicationListener.addPropertySources(ConfigFileApplicationListener.java:158)
       at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEnvironmentPreparedEvent(ConfigFileApplicationListener.j
va:138)
       at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEnvironmentPreparedEvent(ConfigFileApplicationListener.j
va:131)
       at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEvent(ConfigFileApplicationListener.java:120)
       at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:98)
       at org.springframework.boot.context.event.EventPublishingRunListener.publishEvent(EventPublishingRunListener.java:100)
       at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:59)
       at org.springframework.boot.SpringApplication.run(SpringApplication.java:277)
       at org.springframework.boot.SpringApplication.run(SpringApplication.java:944)
       at org.springframework.boot.SpringApplication.run(SpringApplication.java:933)
       at sample.simple.SampleSimpleApplication.main(SampleSimpleApplication.java:46)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
       at java.lang.reflect.Method.invoke(Unknown Source)
       at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
       at java.lang.Thread.run(Unknown Source)
aused by: java.io.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist
       at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
       at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:132)
       at org.springframework.core.io.support.PropertiesLoaderUtils.loadProperties(PropertiesLoaderUtils.java:121)
       at org.springframework.boot.env.PropertiesPropertySourceLoader.load(PropertiesPropertySourceLoader.java:44)
       at org.springframework.boot.env.PropertySourcesLoader.load(PropertySourcesLoader.java:126)
       at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.loadIntoGroup(ConfigFileApplicationListener.java:360)
       at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:349)
       at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:317)
       at org.springframework.boot.context.config.ConfigFileApplicationListener.addPropertySources(ConfigFileApplicationListener.java:155)
       ... 16 common frames omitted

【问题讨论】:

  • application.properties 应该在src\main\resources 文件夹中

标签: java spring spring-boot


【解决方案1】:

当您尝试从 jar 文件运行应用程序时,bug in Spring Boot 1.1.2 会导致 Windows 上的类加载出现一些问题。我建议在修复之前回退到 1.1.1。

【讨论】:

【解决方案2】:

您缺少application.properties 文件。使用 Spring Boot,您可以将此文件放在与 jar 文件相同的级别,它将使用该文件。查看文档以获取更多信息link here

【讨论】:

  • 但是 spring 在类路径中搜索属性文件为 springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)。而 application.properties 文件位于 jar 文件中意味着它在 classpath/