0
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

Samir
  • 1
  • 2
  • I think if you're looking for particular icons to use as resources, it would probably be not such a good idea to bank on finding those resources 'automatically' in your current runtime. So it would be better to package them with your app as your own resources. A quick workaround would be to include in your classpath resources.jar of your Java 8 runtime. It should find the icons then – g00se Oct 10 '22 at 12:56
  • I think you need to tinker the VM arguments a bit to "opens" the desktop module: `--opens java.desktop/javax.swing.plaf.metal=ALL-UNNAMED`. You probably don't have access to the icons from the JDK package at all. – NoDataFound Oct 10 '22 at 16:18

2 Answers2

1

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.

Dhaval Gajjar
  • 2,508
  • 1
  • 2
  • 13
  • *In JDK 11 please use ClassLoader to load resources.* Why do you say that? *Also, omit the first / as we need a relative path, not an absolute one.* Why do you say that? – g00se Oct 10 '22 at 12:08
  • Still getting null – Samir Oct 10 '22 at 12:17
  • Can you try accessing other resources, not of javax package? Some other library resource which is there in your project? – Dhaval Gajjar Oct 10 '22 at 12:30
  • I am able to access my resources, but not the jdk/jre – Samir Oct 10 '22 at 12:32
  • @g00se thanks for pointing that out. I ran the code but got a null value so I used getClassLoader and with a different resource which worked so added that in the answer. – Dhaval Gajjar Oct 10 '22 at 12:49
  • But I can find the icon under java.desktop module – Samir Oct 10 '22 at 12:56
  • Then please use the correct updated path you will get the resource. If the path is wrong or jar doesn't exist in the classpath then it will return null. – Dhaval Gajjar Oct 10 '22 at 13:00
  • *I ran the code but got a null value so I used getClassLoader and with a different resource...* It shouldn't make any difference ```Class.getResource``` delegates to the classloader anyway – g00se Oct 10 '22 at 13:08
  • *But I can find the icon under java.desktop module* It's conceivable that you might need to ```requires``` that module as part of a modular app, but I'm not certain of that – g00se Oct 10 '22 at 13:26
  • I not using module-info.java, using unnamed module i believe it's called as the module-info.java is giveing me error for read from more than one package – Samir Oct 10 '22 at 13:28
1

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
}
Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93
  • Cool, but what if I need to convert that to URL, because I am using that in an ImageIcon which is expecting a URL – Samir Oct 10 '22 at 13:51
  • new File(String.valueOf(p1)).toURI().toURL(); – Samir Oct 10 '22 at 13:53
  • *...because I am using that in an ImageIcon which is expecting a URL* Well of course you could always load the ```Image``` for that ```ImageIcon``` from an input stream too. I'd have to reiterate my earlier point though about relying on the default runtime for icons – g00se Oct 10 '22 at 14:19
  • 2
    @Samir don’t use `new File(…)` when using a `Path`. Generally, don’t use it for embedded resources. This never worked in Java. You can simply call `.toUri().toURL()` on the `Path` instances. – Holger Oct 10 '22 at 16:30
  • 2
    You can, by the way, also use `new URL("jrt:/java.desktop/com/sun/java/swing/plaf/windows/icons/ListView.gif")`, `new URL("jrt:/java.desktop/javax/swing/plaf/metal/icons/ocean/expanded.gif")` in the first place, if you’re interested in getting the `URL` objects only. However, there is no guaranty that these resources exist in a particular runtime… – Holger Oct 10 '22 at 16:53