Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Path component should be '/'

Tags:

I'm trying to create a FileSystem object to hold an ext2 filesystem. My URI seems to be invalid, giving me a path component should be '/' run time error.

I'm using Windows and have my project in Eclipse, with a subdirectory called "fs" that holds the filesystem 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 have loaded the filesystem as a File object and used the getURI method to make sure my URI is the same as the actual URI, and it is.

How can I get the filesystem loaded?

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)
like image 929
user155410 Avatar asked Nov 22 '14 15:11

user155410


2 Answers

The WindowsFileSystemProvider's checks that the URI's path is only '/'. The uri is perfectly valid as URI, the problem is the FileSystem's requisites. crashystar has it right (I can't comment yet) and a Path should be used. If you read the JavaDoc of newFileSystem(Path, ClassLoader) you'll see the ClassLoader can be left at null, so you just need to do

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

By leaving it at null Java tries to locate an installed provider (so you could not expect a custom provider to be used). If it were a custom provider you'd have to use a ClassLoader that can load that provider. If the provider is on your classpath, it'd be enough to do

getClass().getClassLoader()

Since you say you just want the OS to do that, leave it at null.

like image 128
dtortola Avatar answered Oct 20 '22 00:10

dtortola


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.

Note the 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
like image 32
ManyQuestions Avatar answered Oct 20 '22 00:10

ManyQuestions