FileSystemNotFoundException产生原因及解决方案

FileSystemNotFoundException 是 Java NIO(New Input/Output)中的异常,属于 java.nio.file 包。当尝试访问一个未注册或不可用的文件系统时,抛出该异常。通常在使用 FileSystems.getFileSystem()Paths.get() 等方法时,如果指定的文件系统没有找到,就会抛出这个异常。

一、产生原因

  1. 访问未创建的文件系统:

    • 原因: 使用 FileSystems.getFileSystem() 获取某个 URI 对应的文件系统时,如果该文件系统尚未创建或未注册,则会抛出 FileSystemNotFoundException
    • 示例:
      URI zipUri = URI.create("jar:file:/example.zip");
      FileSystem fs = FileSystems.getFileSystem(zipUri); // 如果文件系统不存在,抛出异常
      
  2. 错误的 URI 或路径:

    • 原因: 在使用 FileSystems.getFileSystem()Paths.get() 方法时,如果提供了错误的 URI 或文件路径,例如指向不存在的文件系统或格式不正确的路径,Java 无法解析并找到对应的文件系统,就会抛出此异常。
    • 示例
      URI invalidUri = URI.create("file:/nonexistent.zip");
      FileSystem fs = FileSystems.getFileSystem(invalidUri); // URI 错误导致找不到文件系统
      
  3. 文件系统尚未注册:

    • 原因: 有些特殊文件系统(如 ZIP 文件系统)需要先通过 FileSystems.newFileSystem() 创建并注册到 JVM 中,如果未提前创建和注册,在访问这些文件系统时会抛出此异常。
    • 示例:
      URI zipUri = URI.create("jar:file:/example.zip");
      FileSystem fs = FileSystems.newFileSystem(zipUri, null); // 必须先创建文件系统
      FileSystem existingFs = FileSystems.getFileSystem(zipUri); // 如果未创建会抛出异常
      
  4. 使用不支持的文件系统类型:

    • 原因: 如果试图访问 JVM 不支持的文件系统类型(如某些远程文件系统协议),Java 无法识别并创建文件系统时,就会抛出 FileSystemNotFoundException
    • 示例:
      URI unsupportedUri = URI.create("ftp://example.com/somefile");
      FileSystem fs = FileSystems.getFileSystem(unsupportedUri); // 不支持的文件系统协议
      

二、解决方案

  1. 创建文件系统:

    • 在尝试获取文件系统之前,先使用 FileSystems.newFileSystem() 创建文件系统。如果文件系统已经存在,直接获取;如果不存在,先创建再获取。
    • 示例:
      URI zipUri = URI.create("jar:file:/example.zip");
      try {
          FileSystem fs = FileSystems.getFileSystem(zipUri);
      } catch (FileSystemNotFoundException e) {
          FileSystem fs = FileSystems.newFileSystem(zipUri, null); // 先创建再获取
      }
      
  2. 检查 URI 和路径:

    • 确保传递给 FileSystems.getFileSystem()Paths.get() 的 URI 或路径是正确的。对于 ZIP 文件等文件系统,使用正确的 jar:file: 协议格式。
    • 示例:
      URI zipUri = URI.create("jar:file:/correct/path/example.zip");
      FileSystem fs = FileSystems.getFileSystem(zipUri); // 使用正确的 URI
      
  3. 使用支持的文件系统类型:

    • 确保使用的是 JVM 支持的文件系统类型。如果使用了不支持的协议或路径类型,可能需要调整代码,使用本地文件系统或其他受支持的文件系统。
    • 示例:
      • 使用本地文件系统代替不支持的远程协议。
  4. 确保文件系统存在:

    • 对于 ZIP 文件等虚拟文件系统,确保文件系统的源文件(如 ZIP 文件)存在且可访问。避免传递不存在的路径或 URI。
    • 示例:
      Path zipPath = Paths.get("/path/to/example.zip");
      if (Files.exists(zipPath)) {
          FileSystem fs = FileSystems.newFileSystem(zipPath, null); // 只有在文件存在时创建
      }
      

三、总结

FileSystemNotFoundException 通常由于访问未创建、未注册或不存在的文件系统引起。常见原因包括未创建文件系统、错误的 URI 或路径、不支持的文件系统类型或文件系统未注册。通过确保文件系统的正确创建、检查 URI 和路径、使用受支持的文件系统类型,可以避免该异常。

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(Paths.java:143) at com.ruoyi.kettle.tools.KettleUtil$KettleEnv.init(KettleUtil.java:448) at com.ruoyi.kettle.tools.KettleUtil.runKettleJob(KettleUtil.java:238) at com.ruoyi.kettle.service.impl.KettleJobServiceImpl.runJobRightNow(KettleJobServiceImpl.java:259) at com.ruoyi.kettle.tools.RedisStreamUtil.readGroup(RedisStreamUtil.java:251) at com.ruoyi.kettle.tools.CommandLineRunnerImpl$1.run(CommandLineRunnerImpl.java:26) 16:51:35.413 [Thread-3] ERROR c.r.k.t.KettleUtil - [init,461] - Kettle环境初始化失败 java.nio.file.FileSystemNotFoundException public static class KettleEnv { public static void init() { try { //增加自定义的插件库 int lengths = StepPluginType.getInstance().getPluginFolders().size(); URL resourceUrl = KettleUtil.class.getClassLoader().getResource(""); if (resourceUrl == null) { throw new FileNotFoundException("无法定位资源目录"); } Path homePath = Paths.get(resourceUrl.toURI()); Path kettleRoot = homePath.resolve("kettlePlugins").normalize(); String pluginPath = String.valueOf(kettleRoot); log.info("kettle插件路径,{}",pluginPath); if (!StepPluginType.getInstance().getPluginFolders().stream().anyMatch(a -> a.getFolder().equals(pluginPath))) { StepPluginType.getInstance().getPluginFolders().add(new PluginFolder(pluginPath, false, true)); } KettleEnvironment.init(); EnvUtil.environmentInit(); log.info("Kettle环境初始化成功"); } catch (Exception e) { e.printStackTrace(); log.error("Kettle环境初始化失败"); } } } kettle插件在D:\IDEA\java_kettle\smart-kettle\RuoYi-master\ruoyi-admin\src\main\resources\kettlePlugins,我希望执行kettle作业时候直接用本地程序的插件,ruoyi框架打jar包部署,提示上面的情况,怎么调整。
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

境里婆娑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值