The Path component must be '/'

I am trying to create a FileSystem object to store an ext2 file system. My URI seems invalid, so I need a path component: // runtime error.

I use Windows and have my own project in Eclipse, with a subdirectory called "fs" that contains a file system image.

My code ...

 URI uri = URI.create("file:/C:/Users/Rosetta/workspace/filesystemProject/fs/ext2"); /* uri holds the path to the ext2 file system itself */ try { FileSystem ext2fs = FileSystems.newFileSystem(uri, null); } catch (IOException ioe) { /* ... code */ } 

I loaded the file system as a File object and used the getURI method to make sure my URI matches the actual URI , and it is.

How to load a downloaded file system?

EDIT:

Stack trace below

 Exception in thread "main" java.lang.IllegalArgumentException: Path component should be '/' at sun.nio.fs.WindowsFileSystemProvider.checkUri(Unknown Source) at sun.nio.fs.WindowsFileSystemProvider.newFileSystem(Unknown Source) at java.nio.file.FileSystems.newFileSystem(Unknown Source) at java.nio.file.FileSystems.newFileSystem(Unknown Source) 
+6
source share
4 answers

WindowsFileSystemProvider checks that the URI path is only '/'. Uri works great as a URI, the problem is with the FileSystem props. crashystar has this right (I can not comment yet) and the Path should be used. If you read the JavaDoc newFileSystem (Path, ClassLoader), you will see that ClassLoader can be left null, so you just need to do

 Path path = Paths.get("C:/Users/Rosetta/workspace/filesystemProject/fs/ext2"); FileSystem ext2fs = FileSystems.newFileSystem(path, null); 

Leaving it in zero Java, it tries to find the installed provider (so you could not expect the custom provider to be used). If it were a custom provider, you would have to use ClassLoader, which can load this provider. If the provider is in your class path, it would be enough to do

 getClass().getClassLoader() 

Since you say you just want the OS to do this, leave it at zero.

+5
source

Why not use a Path object?

 newFileSystem(Path path, ClassLoader loader) Constructs a new FileSystem to access the contents of a file as a file system. 

Pay attention to three constructors:

 static FileSystem newFileSystem(Path path, ClassLoader loader) Constructs a new FileSystem to access the contents of a file as a file system. static FileSystem newFileSystem(URI uri, Map<String,?> env) Constructs a new file system that is identified by a URI static FileSystem newFileSystem(URI uri, Map<String,?> env, ClassLoader loader) Constructs a new file system that is identified by a URI 
+4
source

you can try the following:

 URI uri = URI.create("jar:file:/C:/Users/Rosetta/workspace/filesystemProject/fs/ext2"); 
+1
source

This worked for me on Windows. Have not tested it in other OS yet

  private void openZip(File runFile) throws IOException { Map<String, String> env = new HashMap<>(); env.put("create", "true"); env.put("encoding", "UTF-8"); System.out.println(runFile.toURI()); Files.deleteIfExists(runFile.toPath()); zipfs = FileSystems.newFileSystem(URI.create("jar:" + runFile.toURI().toString()), env); //zipfs = FileSystems.newFileSystem(runFile.toPath(), getClass().getClassLoader()); //-----does not work //zipfs = FileSystems.newFileSystem(URI.create("jar:file:/c:/Users/Siraj/Documents/AAAExport4.zip"), env); //---works } 
+1
source

Source: https://habr.com/ru/post/978544/


All Articles