在尝试使用我的java应用程序将jar文件中的某些文件复制到临时目录时,会抛出以下异常:
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)
....
Run Code Online (Sandbox Code Playgroud)
这是我的一小部分setupTempFiles
(带行号):
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);
Run Code Online (Sandbox Code Playgroud)
该webViewPresentation
目录位于我的jar的根目录中:
当我将我的应用程序打包为jar时,此问题才会退出,在Eclipse中进行调试没有问题.我怀疑这与这个bug有关,但我不确定如何纠正这个问题.
任何帮助赞赏
如果事项:
我在Java 8 build 1.8.0-b132上
Windows 7 Ult.64位
fge*_*fge 51
A FileSystemNotFoundException
表示无法自动创建文件系统; 你还没有在这里创建它.
鉴于你的URI,你应该做的是拆分!
,使用之前的部分打开文件系统,然后从以下部分获取路径!
:
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]);
Run Code Online (Sandbox Code Playgroud)
需要注意的是,你应该.close()
你FileSystem
一旦你用它做.
接受的答案不是最好的,因为当您在 IDE 中启动应用程序或资源是静态的并存储在类中时它不起作用!从资源文件夹中获取文件时,在java.nio.file.FileSystemNotFoundException中提出了更好的解决方案
InputStream in = getClass().getResourceAsStream("/webViewPresentation");
byte[] data = IOUtils.toByteArray(in);
Run Code Online (Sandbox Code Playgroud)
IOUtils 来自 Apache commons-io。
但是如果你已经在使用 Spring 并且想要一个文本文件,你可以将第二行更改为
StreamUtils.copyToString(in, Charset.defaultCharset());
Run Code Online (Sandbox Code Playgroud)
StreamUtils.copyToByteArray 也存在。
这可能是一个黑客,但以下对我有用:
URI uri = getClass().getResource("myresourcefile.txt").toURI();
if("jar".equals(uri.getScheme())){
for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
if (provider.getScheme().equalsIgnoreCase("jar")) {
try {
provider.getFileSystem(uri);
} catch (FileSystemNotFoundException e) {
// in this case we need to initialize it first:
provider.newFileSystem(uri, Collections.emptyMap());
}
}
}
}
Path source = Paths.get(uri);
Run Code Online (Sandbox Code Playgroud)
这使用了 ZipFileSystemProvider 在内部存储由 URI 打开的文件系统列表的事实。
归档时间: |
|
查看次数: |
27730 次 |
最近记录: |