Results 1 to 4 of 4
- 04-14-2012, 09:07 PM #1
Senior Member
- Join Date
- Apr 2012
- Posts
- 199
- Rep Power
- 0
Issue with getClass().getClassLoader().getResource(...) finding image file URL
I'm using the NetBeans IDE.
My program is using the following lines of code to load an image:
Java Code:public CaptureImagePosition() throws IOException { super(); URL ImageURL = getClass().getClassLoader().getResource("Tomato1.gif"); TomatoImage = ImageIO.read(ImageURL);
I noticed that the results of ImageURL will only be non-null when I put the image in the ...\CaptureImagePosition\src directory
I also have the Tomato1.gif image stored in a directory called \CaptureImagePosition\src\captureimageposition\res ources
Whys isn't the image being loaded from that location?
I have also noticed that the Tomato1.gif image is in two other locations:
....\CaptureImagePosition\build\classes\captureima geposition\resources
in the CaptureImagePosition.jar file in the ....\CaptureImagePosition\dist directory.
- 04-15-2012, 03:30 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: Issue with getClass().getClassLoader().getResource(...) finding image file URL
The image will be read from the location specified by the expression "getClass().getClassLoader().getResource("Tomato1. gif")". There are two ways to understand what the value of that expression will be. One is to read the getResource() API documentation. The other is more pragmatic: use System.out.println() to print the url you are going to use to load the image.I also have the Tomato1.gif image stored in a directory called \CaptureImagePosition\src\captureimageposition\res ources
Whys isn't the image being loaded from that location?
There is no reason to think that the runtime will regard the resources directory (or jar entry) as special in any way. To repeat what I said before, the "L" in URL means what it says: the value of ImageURL will be the location that is used. (By the way, the variable should be imageURL.)I also have the Tomato1.gif image stored in a directory called \CaptureImagePosition\src\captureimageposition\res ources
Whys isn't the image being loaded from that location?
A rather old JavaWorld article discusses the distinction between the getResource() methods in Class and ClassLoader which you should be aware of.
IDEs will copy source files in the course of building executable code or the jar file for distribution.I have also noticed that the Tomato1.gif image is in two other locations:
....\CaptureImagePosition\build\classes\captureima geposition\resources
in the CaptureImagePosition.jar file in the ....\CaptureImagePosition\dist directory.
- 04-15-2012, 04:08 AM #3
Senior Member
- Join Date
- Apr 2012
- Posts
- 199
- Rep Power
- 0
Re: Issue with getClass().getClassLoader().getResource(...) finding image file URL
Ok, if I use in getClass().getClassLoader().getResource("captureim ageposition/resources/Tomato1.gif") which is relative to my source file directory (=src), it finds the image.
Also, I found out that I could do it another way and just use getClass().getClassLoader().getResource("Tomato1.g if"). The other way is to right click on the project and select properties. Then, add the resource folder (=\CaptureImagePosition\src\captureimageposition\r esources) to the source package folders.
Thanks.
- 04-16-2012, 10:34 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Similar Threads
-
getClass().getResource()
By forwardbias in forum AWT / SwingReplies: 11Last Post: 03-29-2012, 03:10 AM -
Java getClass()
By hqt in forum New To JavaReplies: 2Last Post: 01-31-2012, 08:44 PM -
How to change FileOutputStream to class.getResource()
By gomdohri in forum New To JavaReplies: 1Last Post: 10-27-2011, 10:13 AM -
ClassLoader getResource Problem
By sh4dyPT in forum New To JavaReplies: 5Last Post: 04-01-2010, 08:47 PM -
Issue With Finding Total Number of Blank Spaces in File
By Cod in forum New To JavaReplies: 2Last Post: 12-10-2009, 12:06 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks