gpt4 book ai didi

Java NIO无法从JRT镜像中读取文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:45:44 25 4
gpt4 key购买 nike

当我们通过 jlink 创建 Java 运行时时,它获取所有 java 类/资源并将它们放入 JRT 图像文件:lib/modules

这是我使用的基本 Maven 项目资源结构:

src
main
resources
dict
xkcd_en

我只是想阅读xkcd_en 文本文件。如果我们查看 JRT 文件,它是:

>> jimage list /path/to/lib/modules
...
Module: main
dict/xkcd_en
...

此外,我已在 module-info 中明确打开它,以防万一:

module main {
opens dict;
// ..rest code omitted
}

我可以读取文件的唯一方法是将其作为输入流获取:

作品:

public static InputStream getResourceAsStream(String resource) {
return FileUtils.class.getResourceAsStream(resource);
}

System.out.println(new BufferedReader(
new InputStreamReader(getResourceAsStream("/dict/xkcd_en")))
.lines().collect(Collectors.joining("\n"))
);

不起作用:

但是如果我试图获取文件 URI 并通过 Java NIO API 读取它,它就不起作用:

public static URL getResourceOrThrow(String resource) {
URL url = FileUtils.class.getResource(resource);
Objects.requireNonNull(url);
return url;
}

1 - Java NIO 找不到文件。但它确实存在,否则 getResource() 返回 null

System.out.println(Paths.get(getResourceOrThrow("/dict/xkcd_en").toURI()));
// /main/dict/xkcd_en

Files.readAllLines(Paths.get(getResourceOrThrow("/dict/xkcd_en").toURI()));

Caused by: java.nio.file.NoSuchFileException: /main/dict/xkcd_en
at java.base/jdk.internal.jrtfs.JrtFileSystem.checkNode(JrtFileSystem.java:494)
at java.base/jdk.internal.jrtfs.JrtFileSystem.getFileContent(JrtFileSystem.java:253)
at java.base/jdk.internal.jrtfs.JrtFileSystem.newInputStream(JrtFileSystem.java:342)
at java.base/jdk.internal.jrtfs.JrtPath.newInputStream(JrtPath.java:631)
at java.base/jdk.internal.jrtfs.JrtFileSystemProvider.newInputStream(JrtFileSystemProvider.java:322)

2 - 如果您将直接使用 FileSystem,则相同:

FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));
System.out.println(fs.getPath("main/dict/xkcd_en"));
// main/dict/xkcd_en

Files.readAllLines(fs.getPath("main/dict/xkcd_en")));

Caused by: java.nio.file.NoSuchFileException: /main/dict/xkcd_en
at java.base/jdk.internal.jrtfs.JrtFileSystem.checkNode(JrtFileSystem.java:494)

3 - Java NIO 甚至不知道什么是 jrt:/ 方案。

Files.readAllLines(Paths.get(getResourceOrThrow("/dict/xkcd_en").toExternalForm()));

Caused by: java.nio.file.InvalidPathException: Illegal char <:> at index 3: jrt:/main/dict/xkcd_en
at java.base/sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
at java.base/sun.nio.fs.WindowsPath.parse(WindowsPath.java:92)
at java.base/sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:229)
at java.base/java.nio.file.Path.of(Path.java:147)
at java.base/java.nio.file.Paths.get(Paths.java:69)

这是 specs JRT FS.

A jrt URL is a hierarchical URI, per RFC 3986, with the syntax

jrt:/[$MODULE[/$PATH]]

where $MODULE is an optional module name and $PATH, if present, is thepath to a specific class or resource file within that module. Themeaning of a jrt URL depends upon its structure:

  • jrt:/$MODULE/$PATH refers to the specific class or resource file named $PATH within the given $MODULE.
  • jrt:/$MODULE refers to all of the class and resource files in the module $MODULE.
  • jrt:/ refers to the entire collection of class and resource files stored in the current run-time image.

所以获得的路径对我来说看起来没问题。我哪里错了?

最佳答案

JRT 文件系统

您引用的 JEP 部分专门处理 URL。如果您进一步阅读,您会发现其中讨论了 JRT 文件系统:

A built-in NIO FileSystem provider for the jrt URL scheme ensures that development tools can enumerate and read the class and resource files in a run-time image by loading the FileSystem named by the URL jrt:/, as follows:

FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));
byte[] jlo = Files.readAllBytes(fs.getPath("modules", "java.base",
"java/lang/Object.class"));

The top-level modules directory [emphasis added] in this filesystem contains one subdirectory for each module in the image. The top-level packages directory [emphasis added] contains one subdirectory for each package in the image, and that subdirectory contains a symbolic link to the subdirectory for the module that defines that package.

如您所见,JRT 文件系统在根目录下有两个目录:modulespackages。这些是作为 JDK-8066492 的一部分添加的他们的目的由那个问题描述。所以问题不在于 NIO API 无法读取 JRT 图像中的资源。问题是:

/main/dict/xkcd_en

真的不存在。资源实际位于:

/modules/main/dict/xkcd_en

JRT 网址

JRT URL 采用以下三种形式之一(所有这些都在您在问题中引用的 JEP 部分中提到):

  1. jrt:/$MODULE/$PATH
  2. jrt:/$MODULE
  3. jrt:/

第一种形式用于访问 JRT 镜像中的特定资源以及我们关心的资源。如您所见,该 URL 不包括上面提到的顶级目录。您可以将 URL 视为始终相对于 modules 目录。


JRT 文件系统提供程序错误

也就是说,如 pointed out通过 @Alan Bateman ,你遇到了一个错误。当您有一个 JRT URL 并尝试将其转换为 Path 时,您应该得到一个指向现有文件的 Path。问题是这种转换没有考虑 modules 目录。

此错误已在 Java 13 中由 JDK-8224946 修复.

关于Java NIO无法从JRT镜像中读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54140750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com