java 路径组件应该是'/'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27078996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:10:15  来源:igfitidea点击:

Path component should be '/'

javapathfilesystemsuri

提问by user155410

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

我正在尝试创建一个FileSystem对象来保存 ext2 文件系统。我的URI似乎无效,给我一个路径组件应该是'/'运行时错误。

I'm using Windows and have my project in Eclipse, with a subdirectory called "fs" that holds the filesystem image.

我正在使用 Windows 并且在 Eclipse 中有我的项目,有一个名为“fs”的子目录,用于保存文件系统映像。

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 Fileobject and used the getURImethod to make sure my URIis the same as the actual URI, and it is.

我已将文件系统作为File对象加载并使用该getURI方法来确保 myURI与实际的URI.

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)

回答by dtortola

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

WindowsFileSystemProvider 会检查 URI 的路径是否仅为“/”。uri 作为 URI 是完全有效的,问题是 FileSystem 的必要条件。crashystar 说得对(我还不能评论),应该使用 Path。如果您阅读 newFileSystem(Path, ClassLoader) 的 JavaDoc,您会看到 ClassLoader 可以保留为 null,因此您只需要执行

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

通过将其保留为 null,Java 会尝试定位已安装的提供程序(因此您不能期望使用自定义提供程序)。如果它是自定义提供程序,则必须使用可以加载该提供程序的 ClassLoader。如果提供者在您的类路径上,那就足够了

getClass().getClassLoader()

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

既然你说你只是想让操作系统这样做,就把它留空。

回答by Siraj K

This worked for me on Windows. Haven't tested it out in other OS yet

这在 Windows 上对我有用。尚未在其他操作系统中测试过

 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  
 }

回答by ManyQuestions

Why not use a Path object?

为什么不使用 Path 对象?

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

回答by i love Hyman

you can try this:

你可以试试这个:

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