java can't read input file!_Java NIO can't read files from JRT image

When we create Java runtime by jlink it takes all java classes/resources and places them into JRT image file: lib/modules.

Here is basic Maven project resources structure I use:

src

main

resources

dict

xkcd_en

I'm just trying to read xkcd_en text file. If we look into JRT file, here it is:

>> jimage list /path/to/lib/modules

...

Module: main

dict/xkcd_en

...

Also I've explicitly opened it in module-info, just in case:

module main {

opens dict;

// ..rest code omitted

}

The only way I could read the file is obtaining it as input stream:

WORKS:

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"))

);

DOESN'T WORK:

But if I'm trying to get the file URI and read it via Java NIO API, it doesn't work:

public static URL getResourceOrThrow(String resource) {

URL url = FileUtils.class.getResource(resource);

Objects.requireNonNull(url);

return url;

}

1 - Java NIO can't find the file. But it definitely does exist, otherwise getResource() returns 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 - The same if you'll use FileSystem directly:

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 doesn't even know what jrt:/ scheme is.

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)

Here is the specs of 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 the path to a specific class or resource file within that module. The meaning 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.

So obtained path looks ok to me. Where am I wrong?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值