NIO文件系统异常分析与解决方法
作者:SunnyJourney
时间:2026-03-15
浏览:2
在使用nio加载文件时,在idea中运行没有问题,但打成jar包后在windows和linux下都有问题:代码语言:javascript代码运行次数:0运行复制publicstaticvoidmain(String[]args)throwsException{//SpringApplication.run(MarketCollectApplication.class,args);URIuri=MarketCollectApplication.cla
本文目录
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 fileSystemProviders = FileSystemProvider.installedProviders(); fileSystemProviders.forEach(p -> { System.out.println(p.getClass()); }); Path path = Paths.get(uri); } 这种情况下在idea中没有问题:代码语言:javascript代码运行次数:0运行复制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环境下: 

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代码运行次数:0运行复制 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代码运行次数:0运行复制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
作者最新文章
CorelDRAW是免费软件吗?详解试用、订阅与一次性购买收费模式
2026-10-30 19:32
PDF怎么免费转成Word并保留内容?转换前后检查要点
2026-10-30 17:50
ArchiveofOurOwn小说快速入口链接
2026-10-30 10:30
冰点还原怎么安装 初始配置步骤
2026-10-28 13:38
漫蛙下载最新版本
2026-10-13 13:42
上一篇:
PS调整图片明暗对比技巧
下一篇:
韩小圈官网入口及观看地址
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多




























