英文:
java.nio.file.ProviderNotFoundException: Provider not found
问题
我遇到了一个找不到提供程序的异常,但不知道如何解决它。以下是我的代码:
@Bean
public FileSystem fileSystem() {
try {
Path path = Paths.get("C:/Users/home/Desktop/mydir");
FileSystem ext2fs = FileSystems.newFileSystem(path, Thread.currentThread().getContextClassLoader());
return ext2fs;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
有谁知道可能是什么问题,以及如何解决呢?
我的想法是,我有这个用于文件系统的bean,稍后我将用它进行CRUD操作,但这个bean是为了如果我以后想要更改存储方式,可以轻松地在这个bean中进行更改,例如,如果我想要使用Google作为存储而不是本地存储,或者类似的情况,只需在这个方法中进行设置。我正在使用Spring Boot 2.3.3。
更新 - 完整异常信息
org.springframework.beans.factory.BeanCreationException: 在类路径资源 [Config.class] 中定义的 bean 'fileSystem' 的创建失败: 通过工厂方法实例化 bean 失败; 嵌套的异常是 org.springframework.beans.BeanInstantiationException: 通过工厂方法 'fileSystem' 实例化 bean 失败; 嵌套的异常是 java.nio.file.ProviderNotFoundException: 找不到提供程序
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:655) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
... (省略部分)
新的更新
Caused by: java.lang.IllegalArgumentException: 路径组件应为 '/'
at sun.nio.fs.WindowsFileSystemProvider.checkUri(WindowsFileSystemProvider.java:69) ~[na:1.8.0_251]
... (省略部分)
(注意:以上翻译中的异常堆栈部分已经进行了简化,以确保内容的可读性,因此可能有部分细节被省略或调整。)
英文:
I´ve got this exception provider not found, but don´t know how to solve it. Here is my code:
@Bean
public FileSystem fileSystem() {
try {
Path path = Paths.get("C:/Users/home/Desktop/mydir");
FileSystem ext2fs = FileSystems.newFileSystem(path, Thread.currentThread().getContextClassLoader());
return ext2fs;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
Does anyone know what could be the problem and how to solve it.
The idea is that i have this bean for filesystem, which i will later use for crud operation, but this bean is because if i later want to change storage that it would be easily just changed in this bean, for example if i want to use google as storage instead of local storage or something like that just set it up in this method. I´m using Spring Boot 2.3.3
Update - complete exception
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fileSystem' defined in class path resource [Config.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.nio.file.FileSystem]: Factory method 'fileSystem' threw exception; nested exception is java.nio.file.ProviderNotFoundException: Provider not found
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:655) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:483) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1336) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1176) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at Application.main(Application.java:10) [classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.nio.file.FileSystem]: Factory method 'fileSystem' threw exception; nested exception is java.nio.file.ProviderNotFoundException: Provider not found
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:650) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
... 20 common frames omitted
Caused by: java.nio.file.ProviderNotFoundException: Provider not found
at java.nio.file.FileSystems.newFileSystem(FileSystems.java:407) ~[na:1.8.0_251]
at Config.fileSystem(FileSystemConfig.java:23) ~[classes/:na]
at Config$$EnhancerBySpringCGLIB$$53bc71a0.CGLIB$fileSystem$0(<generated>) ~[classes/:na]
at Config$$EnhancerBySpringCGLIB$$53bc71a0$$FastClassBySpringCGLIB$$4ee51dad.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at Config$$EnhancerBySpringCGLIB$$53bc71a0.fileSystem(<generated>) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_251]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_251]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_251]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_251]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
... 21 common frames omitted
New update
Caused by: java.lang.IllegalArgumentException: Path component should be '/'
at sun.nio.fs.WindowsFileSystemProvider.checkUri(WindowsFileSystemProvider.java:69) ~[na:1.8.0_251]
at sun.nio.fs.WindowsFileSystemProvider.getFileSystem(WindowsFileSystemProvider.java:86) ~[na:1.8.0_251]
at java.nio.file.FileSystems.getFileSystem(FileSystems.java:221) ~[na:1.8.0_251]
at FileSystemConfig.fileSystem(FileSystemConfig.java:23) ~[classes/:na]
at FileSystemConfig$$EnhancerBySpringCGLIB$$53bc71a0.CGLIB$fileSystem$0(<generated>) ~[classes/:na]
at FileSystemConfig$$EnhancerBySpringCGLIB$$53bc71a0$$FastClassBySpringCGLIB$$4ee51dad.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at FileSystemConfig$$EnhancerBySpringCGLIB$$53bc71a0.fileSystem(<generated>) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_251]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_251]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_251]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_251]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
... 21 common frames omitted
答案1
得分: 3
你选择了使用newFileSystem()
方法,并将类对象作为第二个参数。这个第二个参数必须是一个文件系统提供者的类,但在这种情况下不是。请使用类似以下方式:
@Bean
public FileSystem fileSystem() {
Path path = Paths.get(URI.create("file:/C:/Users/home/Desktop/mydir"));
final URI uri = URI.create("file:" + path.toUri().getPath());
FileSystem ext2fs = FileSystems.getFileSystem(uri);
return ext2fs;
}
英文:
You chose the newFileSystem()
method with a class object as second parameter. This second parameter must be a class of an file systems provider which is not in this case. Please just use something like:
@Bean
public FileSystem fileSystem() {
Path path = Paths.get(URI.create("file:/C:/Users/home/Desktop/mydir"));
final URI uri = URI.create("file:" + path.toUri().getPath());
FileSystem ext2fs = FileSystems.getFileSystem(uri);
return ext2fs;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论