Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set DefaultFileSystemProvider for testing

Tags:

java

nio

jimfs

How can I set the DefaultFileSystemProvider to use, for example, JimfsFileSystemProvider? The javadoc for FileSystems.getDefault() says I need to set a system property, but when I try to do that I get a NoSuchMethodException:

System.setProperty("java.nio.file.spi.DefaultFileSystemProvider",
                   "com.google.common.jimfs.JimfsFileSystemProvider");
FileSystems.getDefault();

Stack Trace:

java.lang.Error: java.lang.NoSuchMethodException: com.google.common.jimfs.JimfsFileSystemProvider.<init>(java.nio.file.spi.FileSystemProvider)
at java.nio.file.FileSystems$DefaultFileSystemHolder.getDefaultProvider(FileSystems.java:128)
....

Do I need to set up something else or is this a bug in jimfs?

like image 529
S1lentSt0rm Avatar asked Sep 10 '14 10:09

S1lentSt0rm


1 Answers

The javadoc of FileSystems.getDefault() states that:

...the default FileSystemProvider is instantiated by invoking a one argument constructor whose formal parameter type is FileSystemProvider.

Since JimfsFileSystemProvider does not have such constructor, you can't set it as the default file system.

This is exactly what the error means that you get:

java.lang.Error: java.lang.NoSuchMethodException: com.google.common.jimfs.JimfsFileSystemProvider.<init>(java.nio.file.spi.FileSystemProvider)

The method <init> is the constructor, and no constructor found with parameters java.nio.file.spi.FileSystemProvider.

like image 78
icza Avatar answered Oct 03 '22 12:10

icza