java nio jar,jar中的Java访问文件导致java.nio.file.FileSystemNotFoundException

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)

....

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);

the webViewPresentation directory resides in the root directory of my jar:

941461600b19190e068316f29c6247e1.png

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.

Any helps appreciated

If matters:

I'm on Java 8 build 1.8.0-b132

Windows 7 Ult. x64

解决方案

A FileSystemNotFoundException means the file system cannot be created automatically; and you have not created it here.

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 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]);

Note that you should .close() your FileSystem once you're done with it.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值