jar中的Java访问文件导致java.nio.file.FileSystemNotFoundException [英] Java access files in jar causes java.nio.file.FileSystemNotFoundException
问题描述
尝试使用我的java应用程序将jar文件中的某些文件复制到临时目录时,会抛出以下异常:
While trying to copy some files in my jar file to a temp directory with my java app, the following exception is thrown:
java.nio.file.FileSystemNotFoundException
at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:171)
at com.sun.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:157)
at java.nio.file.Paths.get(Unknown Source)
at com.sora.util.walltoggle.pro.WebViewPresentation.setupTempFiles(WebViewPresentation.java:83)
....
这是我的 setupTempFiles的一小部分
(包含行号):
and this is a small part of my setupTempFiles
(with line numbers):
81. URI uri = getClass().getResource("/webViewPresentation").toURI();
//prints: URI->jar:file:/C:/Users/Tom/Dropbox/WallTogglePro.jar!/webViewPresentation
82. System.out.println("URI->" + uri );
83. Path source = Paths.get(uri);
webViewPresentation
目录位于根目录我的罐子:
the webViewPresentation
directory resides in the root directory of my jar:
此问题仅在我将应用程序打包为jar时退出,在Eclipse中进行调试没有问题。我怀疑这与此错误有关,但我是不确定如何纠正这个问题。
This problem only exits when I package my app as a jar, debugging in Eclipse has no problems. I suspect that this has something to do with this bug but I'm not sure how to correct this problem.
任何帮助表示赞赏
如果事项:
我在使用Java 8 build 1.8.0-b132
I'm on Java 8 build 1.8.0-b132
Windows 7 Ult。 x64
Windows 7 Ult. x64
推荐答案
FileSystemNotFoundException
表示无法自动创建文件系统;并且你还没有在这里创建它。
A FileSystemNotFoundException
means the file system cannot be created automatically; and you have not created it here.
鉴于你的URI,你应该做的是分解!
,使用之前的部分打开文件系统,然后从之后的部分获取路径!
:
Given your URI, what you should do is split against the !
, open the filesystem using the part before it and then get the path from the part after the !
:
final Map<String, String> env = new HashMap<>();
final String[] array = uri.toString().split("!");
final FileSystem fs = FileSystems.newFileSystem(URI.create(array[0]), env);
final Path path = fs.getPath(array[1]);
请注意,您应该 .close()
一旦你完成了它,你的文件系统
。
Note that you should .close()
your FileSystem
once you're done with it.
这篇关于jar中的Java访问文件导致java.nio.file.FileSystemNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!