Rocky
Rocky

Reputation: 415

Issue instantiating a custom java (nio) file system

I am working on creating a custom file system based on the link. I have created a CustomFileSystemProvider (and other CustomFileSystem classes) and overridden the required methods. The getScheme() method of the CustomFileSystemProvider returns the string "myfs". However when I try to test this file system with the following code

FileSystem fs = FileSystems.newFileSystem(URI.create("myfs://a_remote_resource"), env);

I get an error

java.nio.file.ProviderNotFoundException: Provider "myfs" not found
at java.nio.file.FileSystems.newFileSystem(FileSystems.java:341)
at java.nio.file.FileSystems.newFileSystem(FileSystems.java:276)
at Tester.main(Tester.java:30)

I don't see my custom file system in the list returned by FileSystemProvider.installedProviders(). I am not sure what I need to register a custom FS as an "installedProvider".

Upvotes: 4

Views: 3699

Answers (3)

mdarwin
mdarwin

Reputation: 2384

You can use Google auto-service for this:

Annotate your FileSystemProvider implementation class as follows:

@AutoService(FileSystemProvider.class)

Then you won't need to fiddle about with files in META-INF.

Upvotes: 1

Qidong Wang
Qidong Wang

Reputation: 1

It's a error URL.Change the URL to a standard url.You also can provide a analysiser.

URL aURL = new URL("http://java.sun.com:80/docs/books/tutorial" + "/index.html?name=networking#DOWNLOADING");

System.out.println("protocol = " + aURL.getProtocol());

System.out.println("authority = " + aURL.getAuthority()); System.out.println("host = " + aURL.getHost());

System.out.println("port = " + aURL.getPort());

System.out.println("path = " + aURL.getPath());

System.out.println("query = " + aURL.getQuery());

System.out.println("filename = " + aURL.getFile());

System.out.println("ref = " + aURL.getRef());

result like :

ut:ut:protocol = http
ut:ut:authority = localhost:8080
ut:ut:host = localhost
ut:ut:port = 8080
ut:ut:path = /UT2.0/login.action
ut:ut:query = null
ut:ut:filename = /UT2.0/login.action
ut:ut:ref = null

If you operate file by internet,choose a kind of a protocol such as RPC,socket...

Upvotes: -4

Lolo
Lolo

Reputation: 4347

A FileSystemProvider is loaded as explained in the Javadoc for FileSystems:

Installed providers are loaded using the service-provider loading facility defined by the ServiceLoader class. Installed providers are loaded using the system class loader. If the system class loader cannot be found then the extension class loader is used; if there is no extension class loader then the bootstrap class loader is used. Providers are typically installed by placing them in a JAR file on the application class path or in the extension directory, the JAR file contains a provider-configuration file named java.nio.file.spi.FileSystemProvider in the resource directory META-INF/services, and the file lists one or more fully-qualified names of concrete subclass of FileSystemProvider that have a zero argument constructor

Upvotes: 4

Related Questions