8

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?

dimo414
  • 47,227
  • 18
  • 148
  • 244
S1lentSt0rm
  • 1,989
  • 2
  • 17
  • 28
  • 1
    Needing to set the default file system is generally an anti-pattern, and intentionally not implemented by the Jimfs team. See [Kevin Bourrillion's comment](https://plus.google.com/+googleguava/posts/a3idqfdnpzC) on Guava's Google+ post announcing Jimfs. – dimo414 Nov 04 '14 at 18:27
  • @dimo414 looks like a broken link – Steve Apr 12 '17 at 17:23
  • @Steve just tried it, it loads fine for me. – dimo414 Apr 12 '17 at 20:11
  • @dimo414 Really.....interesting. Maybe it's something with the proxy/firewall settings where I work. Sorry about that. – Steve Apr 12 '17 at 20:32
  • Kevin Bourrillion said: "We don't think that is the right way to go -- mutating global static state causes a huge mess. Instead, stop writing code that depends on the default file system. When code can accept a Path and work off its implicit fs, great; otherwise have the FileSystem itself injected. (Also, stop writing code that depends on the system default anything! )" – Sergey Ponomarev Feb 06 '19 at 00:09

1 Answers1

4

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.

icza
  • 389,944
  • 63
  • 907
  • 827