JIMFS不能被ZipFileSystemProvider识别 [英] JIMFS not recognized by ZipFileSystemProvider
问题描述
我使用字节数组在 jimfs (内存文件系统中的Google)中创建了一个zip文件。当试图使用 ZipMemoryFileSystem
打开该文件时,出现提供程序无法识别的错误。
我的代码如下:
public static void test(byte [] document){
try {
try(FileSystem memoryFileSystem = Jimfs.newFileSystem(Configuration.unix())){
Files.write(memoryFileSystem.getPath(/ file.zip),document);
URI uri = URI.create(jar:+ memoryFileSystem.getPath(/ file.zip)。toUri());
Map< String,String> env = Collections.singletonMap(create,false);
try(FileSystem zipfs = FileSystems.newFileSystem(uri,env)){
//做某事
} catch(Exception e){
e.printStackTrace();
$ b} catch(IOException e){
e.printStackTrace();
URI类似于: jar:jimfs://bb2c779f-d327-4e2f-9841-bd04785f1365/file.zip
。
堆栈跟踪是: p>
java.nio.file.FileSystemNotFoundException:提供程序jimfs未在java.nio.file.Paths.get中安装
(Paths.java:158)
在com.sun.nio.zipfs.ZipFileSystemProvider.uriToPath(ZipFileSystemProvider.java:97)
在com.sun.nio.zipfs.ZipFileSystemProvider.newFileSystem(ZipFileSystemProvider.java :119)
在java.nio.file.FileSystems.newFileSystem(FileSystems.java:337)
在java.nio.file.FileSystems.newFileSystem(FileSystems.java:287)
at office.ImfsTest.test(ImfsTest.java:88)
at office.ImfsTest.main(ImfsTest.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:95)
at sun.reflect.DelegatingMethodAccessorImpl.in voke(DelegatingMethodAccessorImpl.java:55)
在java.lang.reflect.Method.invoke(Method.java:508)
在org.codehaus.mojo.exec.ExecJavaMojo $ 1.run(ExecJavaMojo.java :293)
at java.lang.Thread.run(Thread.java:785)
jimfs未列出 FileSystemProvider.installedProviders()
。它在一个单独的类加载器中,分别是 ZipFileSystemProvider
,分别是 Thread.currentThread()。getContextClassLoader()
vs. ClassLoader.getSystemClassLoader()。getParent()
用于 FileSystemZipProvider
。
提供商打印如下:
IM提供商:com。 google.common.jimfs.JimfsFileSystemProvider@ed301b1f IM方案:jimfs IM类装载器:java.net.URLClassLoader@4940e7a2
安装提供:sun.nio.fs.LinuxFileSystemProvider@d83a6d85方案:文件类装载器:空
中安装提供者:com.sun.nio.zipfs.ZipFileSystemProvider@110a4ec7体系:罐子类装载器:sun.misc.Launcher$ExtClassLoader@59a155ab
我试图根据jimfs手动设置类加载器。 ClassLoaderTest 示例没有成功。
我正在Linux上运行。
使用jimfs 1.1(也试过2.0-SNAPSHOT修复 https://github.com/google/jimfs/commit/3299e69f75cf524e6d101d88e8c202c1b24bf25a 以问题
$ b 如何让我的代码工作?
解决方案:
基于SO问题解决方案: / 36161795 /如何注册一个spi实现当运行execjava>如何注册一个SPI实现时运行exec:java ,我知道类加载器的问题是由于事实代码使用 maven exec:java 运行。
使用普通的jar来运行代码解决了这个问题!
I have a zip file created in jimfs (google in memory file system) from a byte array. When trying to open that file with ZipMemoryFileSystem
, I get an error that the provider is not recognized.
My code is as following:
public static void test(byte[] document) {
try {
try (FileSystem memoryFileSystem = Jimfs.newFileSystem(Configuration.unix())) {
Files.write(memoryFileSystem.getPath("/file.zip"), document);
URI uri = URI.create("jar:" + memoryFileSystem.getPath("/file.zip").toUri());
Map<String, String> env = Collections.singletonMap("create", "false");
try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
//do something
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
The URI is something like: jar:jimfs://bb2c779f-d327-4e2f-9841-bd04785f1365/file.zip
.
The stack trace is:
java.nio.file.FileSystemNotFoundException: Provider "jimfs" not installed
at java.nio.file.Paths.get(Paths.java:158)
at com.sun.nio.zipfs.ZipFileSystemProvider.uriToPath(ZipFileSystemProvider.java:97)
at com.sun.nio.zipfs.ZipFileSystemProvider.newFileSystem(ZipFileSystemProvider.java:119)
at java.nio.file.FileSystems.newFileSystem(FileSystems.java:337)
at java.nio.file.FileSystems.newFileSystem(FileSystems.java:287)
at office.ImfsTest.test(ImfsTest.java:88)
at office.ImfsTest.main(ImfsTest.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:95)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
at java.lang.reflect.Method.invoke(Method.java:508)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:293)
at java.lang.Thread.run(Thread.java:785)
The jimfs is not listed with FileSystemProvider.installedProviders()
. It is in a separate class loader than ZipFileSystemProvider
, respectively Thread.currentThread().getContextClassLoader()
vs. ClassLoader.getSystemClassLoader().getParent()
for FileSystemZipProvider
.
Providers prints are as following:
IM Provider:com.google.common.jimfs.JimfsFileSystemProvider@ed301b1f IM Scheme:jimfs IM Class Loader:java.net.URLClassLoader@4940e7a2
Installed Provider:sun.nio.fs.LinuxFileSystemProvider@d83a6d85 Scheme:file Class Loader:null
Installed Provider:com.sun.nio.zipfs.ZipFileSystemProvider@110a4ec7 Scheme:jar Class Loader:sun.misc.Launcher$ExtClassLoader@59a155ab
I have tried to set the class loaders manually based on jimfs ClassLoaderTest example with no success.
I am running on Linux.
Used jimfs 1.1 (also tried 2.0-SNAPSHOT with fix https://github.com/google/jimfs/commit/3299e69f75cf524e6d101d88e8c202c1b24bf25a for issue 31).
How could I have my code working?
解决方案 Solution:
Based on SO issue How to register a SPI implementation when running exec:java, I understood the class loader problem was due to the fact the code was run with maven exec:java.
Running the code using a plain jar solved the problem!
这篇关于JIMFS不能被ZipFileSystemProvider识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!