I need to get a resource image file in a java project. What I'm doing is:
URL url = TestGameTable.class.getClass(). getClassLoader().getResource("unibo.lsb.res/dice.jpg");
The directory structure is the following:
unibo/ lsb/ res/ dice.jpg test/ ..../ /* other packages */
The fact is that I always get as the file doesn't exist. I have tried many different paths, but I couldn't solve the issue. Any hint?
Java programs can use two mechanisms to access resources: Applets use Applet. getCodeBase() to get the base URL for the applet code and then extend the base URL with a relative path to load the desired resource, for example with Applet. getAudioClip(url) . Applications use "well known locations" such as System.
The Android resource system keeps track of all non-code assets associated with an application. You can use this class to access your application's resources. You can generally acquire the Resources instance associated with your application with getResources() .
This works when running inside and outside of a Jar file. PathMatchingResourcePatternResolver r = new PathMatchingResourcePatternResolver(); Resource[] resources = r. getResources("/myfolder/*"); Then you can access the data using getInputStream and the filename from getFilename .
TestGameTable.class.getResource("/unibo/lsb/res/dice.jpg");
getResource()
directly on the class.Instead of explicitly writing the class name you could use
this.getClass().getResource("/unibo/lsb/res/dice.jpg");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With