Skip to content

"file:." cannot be resolved to java.nio.file.Path (and plain "." value resolves to classpath root) #33124

@ewoerner

Description

@ewoerner

Given the following stripped-down demo application:

@SpringBootApplication
public class InjectedPathDemoApplication implements ApplicationRunner {
	@Value("${dir}")
	private Path dir;

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

	@Override
	public void run(ApplicationArguments args) throws Exception {
		System.out.println(dir);
	}
}

When run using a JDK in Windows:

$ java -jar demo.jar --dir=..
..
$ java -jar demo.jar --dir=./test
test
$ java -jar demo.jar --dir=.
C:\■■■■■■■■\injected-path-demo\bin\main

When run as native binary compiled with GraalVM:

$ ./demo-native --dir=..
..
$ ./demo-native --dir=./test
test
$ ./demo-native --dir=.
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'injectedPathDemoApplication': Unsatisfied dependency expressed through field 'dir': Failed to convert value of type 'java.lang.String' to required type 'java.nio.file.Path'; Could not retrieve file for class path resource []: class path resource [] cannot be resolved to absolute file path because it does not reside in the file system: resource:/
Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'injectedPathDemoApplication': Unsatisfied dependency expressed through field 'dir': Failed to convert value of type 'java.lang.String' to required type 'java.nio.file.Path'; Could not retrieve file for class path resource []: class path resource [] cannot be resolved to absolute file path because it does not reside in the file system: resource:/
        at org.springframework.beans.factory.aot.AutowiredFieldValueResolver.resolveValue(AutowiredFieldValueResolver.java:194)
        at org.springframework.beans.factory.aot.AutowiredFieldValueResolver.resolveAndSet(AutowiredFieldValueResolver.java:167)
        at com.example.demo.InjectedPathDemoApplication__Autowiring.apply(InjectedPathDemoApplication__Autowiring.java:17)
        at org.springframework.beans.factory.support.InstanceSupplier$1.get(InstanceSupplier.java:83)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.obtainInstanceFromSupplier(DefaultListableBeanFactory.java:949)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.obtainFromSupplier(AbstractAutowireCapableBeanFactory.java:1219)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1162)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:562)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522)
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:337)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:335)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:975)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:962)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:624)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:335)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1363)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1352)
        at com.example.demo.InjectedPathDemoApplication.main(InjectedPathDemoApplication.java:17)
Caused by: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.nio.file.Path'; Could not retrieve file for class path resource []: class path resource [] cannot be resolved to absolute file path because it does not reside in the file system: resource:/
        at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:87)
        at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:71)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1381)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1353)
        at org.springframework.beans.factory.aot.AutowiredFieldValueResolver.resolveValue(AutowiredFieldValueResolver.java:188)
        ... 21 more
Caused by: java.lang.IllegalArgumentException: Could not retrieve file for class path resource []: class path resource [] cannot be resolved to absolute file path because it does not reside in the file system: resource:/
        at org.springframework.beans.propertyeditors.PathEditor.setAsText(PathEditor.java:115)
        at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:439)
        at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:412)
        at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:161)
        at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:80)
        ... 25 more

Expected result for the third call: returns either "." or "", and does not throw an error on native

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchestype: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions