Home > Software engineering >  Get Resource in openjdk 11
Get Resource in openjdk 11

Time:10-10

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?

I have tried getClass(), ClassLoader, but I am getting null.

Error Image

CodePudding user response:

In OpenJDK 11 don't include that resources.jar library by default. So you have to manually add the resources jar file.

If you check the java 8 lib directory there is a resources.jar file that contains all those icons. In OpenJDK 11 it does not exist. like XML bind and other libraries removed from the standard java 11 package.

Please add external library and use your code to get resource.

CodePudding user response:

You have to use the jrt file system to access embedded resources:

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.

From there you can interact with the paths using the methods in the Files class. For instance:

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