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!