前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >nio使用中的java.nio.file.FileSystemNotFoundException分析析

nio使用中的java.nio.file.FileSystemNotFoundException分析析

作者头像
山行AI
发布2019-06-28 16:38:57
4.7K0
发布2019-06-28 16:38:57
举报
文章被收录于专栏:山行AI

在使用nio加载文件时,在idea中运行没有问题,但打成jar包后在windows和linux下都有问题:

代码语言:javascript
复制
  public static void main(String[] args) throws Exception{        // SpringApplication.run(MarketCollectApplication.class,args);         URI uri = MarketCollectApplication.class.getClassLoader().getResource("conf/sh.txt").toURI();         FileSystem aDefault = FileSystems.getDefault();         System.out.println(aDefault.getClass());         FileSystemProvider provider = FileSystems.getDefault().provider();         System.out.println(provider.getClass());         System.out.println("====================" + uri.getScheme());         List<FileSystemProvider> fileSystemProviders = FileSystemProvider.installedProviders();         fileSystemProviders.forEach(p -> {             System.out.println(p.getClass());         });         Path path = Paths.get(uri);     }

这种情况下在idea中没有问题:

代码语言:javascript
复制
class sun.nio.fs.WindowsFileSystemclass sun.nio.fs.WindowsFileSystemProvider====================fileclass sun.nio.fs.WindowsFileSystemProviderclass com.sun.nio.zipfs.ZipFileSystemProvider

但是在打成jar包运行时Path path = Paths.get(uri)这一行会抛出异常:

  • windows环境下:
  • linux环境下:

究其原因,是FileSystemProvider的使用问题,先看java.nio.file.Paths#get(java.net.URI):

代码语言:javascript
复制
public static Path get(URI uri) {        String scheme =  uri.getScheme();        if (scheme == null)            throw new IllegalArgumentException("Missing scheme");
        // check for default provider to avoid loading of installed providers        if (scheme.equalsIgnoreCase("file"))            return FileSystems.getDefault().provider().getPath(uri);
        // try to find provider        for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {            if (provider.getScheme().equalsIgnoreCase(scheme)) {                return provider.getPath(uri);            }        }
        throw new FileSystemNotFoundException("Provider \"" + scheme + "\" not installed");    }
  • uri.getScheme()在idea中是file,在打成jar包后变成了jar。
  • 当前缀以file开头时,会使用FileSystems.getDefault().provider()来处理,这个provider在windows环境下是WindowsFileSystemProvider, 在linux环境下是LinuxFileSystemProvider。
  • FileSystemProvider.installedProviders()对应windows中的WindowsFileSystemProvider和ZipFileSystemProvider,对应linux中的LinuxFileSystemProvider和ZipFileSystemProvider。
  • 当前缀不以file开头时,会使用FileSystemProvider.installedProviders()中与uri.getScheme()匹配的provider来处理,对应的就是ZipFileSystemProvider。
  • ZipFileSystemProvider对应的FileSystem需要自己创建,使用和创建方式参考:https://docs.oracle.com/javase/8/docs/technotes/guides/io/fsp/zipfilesystemprovider.html

解决办法:在Path path = Paths.get(uri)中进行处理

代码语言:javascript
复制
    Path path = null;    try{        path = Paths.get(uri);    }catch (Exception e){        //@see https://stackoverflow.com/questions/25032716/getting-filesystemnotfoundexception-from-zipfilesystemprovider-when-creating-a-p        //@see  http://docs.oracle.com/javase/7/docs/technotes/guides/io/fsp/zipfilesystemprovider.html        FileSystem zipfs = FileSystems.newFileSystem(uri, env);        path = Paths.get(uri);    }

或者使用其他办法加载资源文件:

代码语言:javascript
复制
byte[] data;try (InputStream in = getClass().getResourceAsStream("/elasticsearch/segmentsIndex.json")) {    data = IOUtils.toByteArray(in);}

参考:https://stackoverflow.com/questions/25032716/getting-filesystemnotfoundexception-from-zipfilesystemprovider-when-creating-a-p

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-06-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 开发架构二三事 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 在使用nio加载文件时,在idea中运行没有问题,但打成jar包后在windows和linux下都有问题:
  • 这种情况下在idea中没有问题:
  • 但是在打成jar包运行时Path path = Paths.get(uri)这一行会抛出异常:
  • 究其原因,是FileSystemProvider的使用问题,先看java.nio.file.Paths#get(java.net.URI):
  • 解决办法:在Path path = Paths.get(uri)中进行处理
  • 参考:https://stackoverflow.com/questions/25032716/getting-filesystemnotfoundexception-from-zipfilesystemprovider-when-creating-a-p
相关产品与服务
文件存储
文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档