简体   繁体   English

在 openjdk 11 中获取资源

[英]Get Resource in openjdk 11

Classx.class.getResource("/com/sun/java/swing/plaf/windows/icons/ListView.gif");
Classx.class.getResource("/javax/swing/plaf/metal/icons/ocean/expanded.gif");

How can I get resource on JDK 11, which is working on JDK 8?我如何获得在 JDK 8 上运行的 JDK 11 的资源?

I have tried getClass(), ClassLoader, but I am getting null.我试过 getClass()、ClassLoader,但我得到的是 null。

Error Image错误图片

You have to use the jrt file system to access embedded resources:您必须使用 jrt 文件系统来访问嵌入式资源:

FileSystem jrt = FileSystems.getFileSystem(URI.create("jrt:/"))
Path p1 = jrt.getPath("/modules/java.desktop/com/sun/java/swing/plaf/windows/icons/ListView.gif");
Path p2 = jrt.getPath("/modules/java.desktop/javax/swing/plaf/metal/icons/ocean/expanded.gif");

Note also that the path needs /modules/java.desktop in front.另请注意,路径前面需要/modules/java.desktop

From there you can interact with the paths using the methods in the Files class. For instance:从那里您可以使用Files class 中的方法与路径交互。例如:

try (InputStream is = Files.newInputStream(p1)) {
    // use is
}

In OpenJDK 11 don't include that resources.jar library by default.在 OpenJDK 11 中,默认情况下不包含 resources.jar 库。 So you have to manually add the resources jar file.所以你必须手动添加资源 jar 文件。

If you check the java 8 lib directory there is a resources.jar file that contains all those icons.如果您检查 java 8 lib 目录,则会有一个包含所有这些图标的resources.jar文件。 In OpenJDK 11 it does not exist.在 OpenJDK 11 中它不存在。 like XML bind and other libraries removed from the standard java 11 package.像 XML 绑定和从标准 java 11 package 中删除的其他库。

Please add external library and use your code to get resource.请添加外部库并使用您的代码获取资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM