打包为app-image后出现FileSystemNotFoundException: Provider 'jar'未安装问题
解决
FileSystemNotFoundException: Provider "jar" not installed异常 错误原因
打包成app-image后,trace.bat会被嵌入到归档(jar或镜像的打包结构)中,此时通过getClassLoader().getResource()获取的URL协议为jar,而Paths.get(res.toURI())和File类仅能处理本地文件系统的路径,无法直接访问归档内的资源,因此抛出该异常。
解决方案
方案1:将资源文件提取到临时目录执行
把归档内的trace.bat复制到系统临时目录,再执行临时文件,执行完成后可自动删除临时文件。示例代码:
URL res = getClass().getClassLoader().getResource("trace.bat"); // 创建临时bat文件,程序退出时自动删除 File tempBat = File.createTempFile("trace", ".bat"); tempBat.deleteOnExit(); // 将资源流复制到临时文件 try (InputStream is = res.openStream()) { Files.copy(is, tempBat.toPath(), StandardCopyOption.REPLACE_EXISTING); } // 执行临时bat文件 String absolutePath = tempBat.getAbsolutePath(); scan = Runtime.getRuntime().exec("cmd /c " + absolutePath + " " + ip);
方案2:直接构造命令替代bat文件
你的trace.bat仅做了编码设置和调用tracert,可以直接在Java代码中构造等效命令,完全不需要依赖bat文件:
// 直接构造cmd命令,实现bat的功能 String command = String.format("cmd /c chcp 65001 && tracert -w 1 %s", ip); scan = Runtime.getRuntime().exec(command);
这种方式更简洁,还能避免资源提取的额外操作,推荐使用。
输入流处理优化
处理输入流时建议指定UTF-8编码,避免中文乱码:
InputStreamReader inputStreamReader = new InputStreamReader(scan.getInputStream(), StandardCharsets.UTF_8);
内容的提问来源于stack exchange,提问作者Fezas
相关产品推荐
相关产品推荐

