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

蓮花仙者
发布: 2025-10-02 10:14:24
原创
772人浏览过

在使用nio加载文件时,在idea中运行没有问题,但打成jar包后在windowslinux下都有问题:代码语言:javascript代码运行次数:0运行复制

<code class="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);     }</code>
登录后复制
这种情况下在idea中没有问题:代码语言:javascript代码运行次数:0运行复制
<code class="javascript">class sun.nio.fs.WindowsFileSystemclass sun.nio.fs.WindowsFileSystemProvider====================fileclass sun.nio.fs.WindowsFileSystemProviderclass com.sun.nio.zipfs.ZipFileSystemProvider</code>
登录后复制
但是在打成jar包运行时Path path = Paths.get(uri)这一行会抛出异常:windows环境下:
nio使用中的java.nio.file.FileSystemNotFoundException分析析
linux环境下:
nio使用中的java.nio.file.FileSystemNotFoundException分析析
究其原因,是FileSystemProvider的使用问题,先看java.nio.file.Paths#get(java.net.URI):代码语言:javascript代码运行次数:0运行复制
<code class="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");    }</code>
登录后复制
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代码运行次数:0运行复制
<code class="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);    }</code>
登录后复制

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

析稿Ai写作
析稿Ai写作

科研人的高效工具:AI论文自动生成,十分钟万字,无限大纲规划写作思路。

析稿Ai写作 97
查看详情 析稿Ai写作
代码语言:javascript代码运行次数:0运行复制
<code class="javascript">byte[] data;try (InputStream in = getClass().getResourceAsStream("/elasticsearch/segmentsIndex.json")) {    data = IOUtils.toByteArray(in);}</code>
登录后复制
参考:https://stackoverflow.com/questions/25032716/getting-filesystemnotfoundexception-from-zipfilesystemprovider-when-creating-a-p

以上就是nio使用中的java.nio.file.FileSystemNotFoundException分析析的详细内容,更多请关注php中文网其它相关文章!

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号