Spring Boot应用启动失败:无法找到指定路径的application.properties
问题描述
使用Eclipse开发Spring Boot项目,此前所有项目运行正常,但现在启动应用时出现错误:系统尝试从桌面路径C:\Users\kashi\Desktop\application.properties读取配置文件,而非项目的resource目录。此前可正常运行的项目及仅包含单个Rest Controller的新项目均出现该问题。已尝试更换工作区、清理本地Maven依赖、重装Eclipse,但仍无法运行任何Spring Boot项目。
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>3.0.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.luv2code.springboot.demo</groupId> <artifactId>mycoolapp</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mycoolapp</name> <description>Demo project for Spring Boot</description> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 支持自动重载 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
控制台错误日志
00:25:43.687 [restartedMain] DEBUG org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter - Application failed to start due to an exception org.springframework.boot.context.config.ConfigDataResourceNotFoundException: Config data resource 'file [C:\Users\kashi\Desktop\application.properties]' via location 'C:/Users/kashi/Desktop/application.properties' cannot be found at org.springframework.boot.context.config.ConfigDataResourceNotFoundException.withLocation(ConfigDataResourceNotFoundException.java:97) at org.springframework.boot.context.config.ConfigDataImporter.handle(ConfigDataImporter.java:145) at org.springframework.boot.context.config.ConfigDataImporter.load(ConfigDataImporter.java:136) at org.springframework.boot.context.config.ConfigDataImporter.resolveAndLoad(ConfigDataImporter.java:86) at org.springframework.boot.context.config.ConfigDataEnvironmentContributors.withProcessedImports(ConfigDataEnvironmentContributors.java:115) at org.springframework.boot.context.config.ConfigDataEnvironment.processInitial(ConfigDataEnvironment.java:242) at org.springframework.boot.context.config.ConfigDataEnvironment.processAndApply(ConfigDataEnvironment.java:229) at org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor.postProcessEnvironment(ConfigDataEnvironmentPostProcessor.java:96) at org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor.postProcessEnvironment(ConfigDataEnvironmentPostProcessor.java:89) at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEnvironmentPreparedEvent(EnvironmentPostProcessorApplicationListener.java:109) at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEvent(EnvironmentPostProcessorApplicationListener.java:94) at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176) at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131) at org.springframework.boot.context.event.EventPublishingRunListener.multicastInitialEvent(EventPublishingRunListener.java:136) at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:81) at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:64) at java.base/java.lang.Iterable.forEach(Iterable.java:75) at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:118) at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:112) at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:63) at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:352) at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1291) at com.luv2code.springboot.demo.mycoolapp.MycoolappApplication.main(MycoolappApplication.java:10) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) 00:25:43.707 [restartedMain] ERROR org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter -
启动失败提示
应用启动失败
描述:
配置数据资源 'file [C:\Users\kashi\Desktop\application.properties]'(路径为 'C:/Users/kashi/Desktop/application.properties')不存在
操作建议:
检查值 'C:/Users/kashi/Desktop/application.properties' 是否正确,或在其前添加前缀 'optional:'
解决方案
- 检查系统环境变量:查看是否设置了
SPRING_CONFIG_LOCATION或SPRING_CONFIG_ADDITIONAL_LOCATION环境变量并指向桌面路径,若有则删除或修改为项目resource目录路径。 - 检查Eclipse运行配置:右键项目 -> Run As -> Run Configurations,找到对应Spring Boot应用配置,查看
Environment标签页是否添加了spring.config.location或spring.config.additional-location变量,若有则删除或修正。 - 检查用户目录配置:查看
C:\Users\kashi目录下是否存在spring-boot-devtools.properties或.spring-boot-devtools.properties文件,里面是否配置了spring.config.location指向桌面路径,若有则修改或删除该文件。 - 检查JVM启动参数:在运行配置的
Arguments标签页的VM arguments中,是否添加了-Dspring.config.location=C:/Users/kashi/Desktop/application.properties这类参数,若有则删除。
内容的提问来源于stack exchange,提问作者Ashish Kumar
相关产品推荐
相关产品推荐

