Results 1 to 4 of 4
  1. #1
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default 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.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Issue with getClass().getClassLoader().getResource(...) finding image file URL

    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?
    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.)

    A rather old JavaWorld article discusses the distinction between the getResource() methods in Class and ClassLoader which you should be aware of.

    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.
    IDEs will copy source files in the course of building executable code or the jar file for distribution.

  3. #3
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default 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.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Issue with getClass().getClassLoader().getResource(...) finding image file URL

    Why are you using the ClassLoader version?
    That's almost invariably unecessary.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. getClass().getResource()
    By forwardbias in forum AWT / Swing
    Replies: 11
    Last Post: 03-29-2012, 03:10 AM
  2. Java getClass()
    By hqt in forum New To Java
    Replies: 2
    Last Post: 01-31-2012, 08:44 PM
  3. Replies: 1
    Last Post: 10-27-2011, 10:13 AM
  4. ClassLoader getResource Problem
    By sh4dyPT in forum New To Java
    Replies: 5
    Last Post: 04-01-2010, 08:47 PM
  5. Replies: 2
    Last Post: 12-10-2009, 12:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •