1

Is there something magic about a folder with the name 'resource' as opposed to something else. I have a file in 'resource' and when I try to load it, it works fine.

ClassLoader loader=Thread.currentThread().getContextClassLoader();
InputStream propFile=loader.getResourceAsStream("KCBSEvents/resource/build.properties");

I do a prop.load(propFile) to load properties.

When I try to use similar logic to load an image from a directory named 'Images', it fails. Both directories are at the same level in my tree.

ClassLoader loader=Thread.currentThread().getContextClassLoader();
java.net.URL logoURL=loader.getClass().getResource("KCBSEvents/Images/KCBSLogo.jpg");

The result is that 'logoURL' is null. I can only conclude that either there is something magic about the directory name 'resource' or 'getResource' works differently than 'getResourceAsStream'. Can someone explain what is going on? TIA.

Wt Riker
  • 514
  • 12
  • 24
  • Well, get resource as stream returns a stream, but maybe related: http://stackoverflow.com/questions/6608795/what-is-the-difference-between-class-getresource-and-classloader-getresource – OneCricketeer Jan 16 '17 at 14:26
  • 1
    Maybe you should put the images folder in the resources folder? – OneCricketeer Jan 16 '17 at 14:27
  • What do you want to have, a URL or? – Grzegorz Górkiewicz Jan 16 '17 at 14:33
  • Why do you call `loader.getClass().getResource(...)` instead of `loader.getResource(...)`? Afaik the first version would assume your path is a relative one which would thus depend on the package the classloader is in and I assume the resulting path `classloader_package/your_path` doesn't exist. If you have a look at the source code of `getResourceAsStream(...)` you'll see that it calls `getResource(...)` and just opens a stream on the resulting url so both methods should do the same - the only difference is whether you call them on a class or on a classloader. – Thomas Jan 16 '17 at 14:34
  • 1
    Thanks for all the replies. I could move the file to the resources folder as a test but ultimately I still need to understand why the difference. I am using getClass because not using it doesn't work either. However, you may be on to something. If I remove that, 'logoURL' is no longer null. But then when I do 'logo.setIcon(new ImageIcon(logoURL));' nothing shows up where the image should be. – Wt Riker Jan 16 '17 at 14:39
  • In that case I'd try and debug whether the image actually can be loaded. You might also want to try and host the image externally for debugging processes, i.e. don't package it into the application until you made sure it is displayed correctly. I'd suspect the image itself is found but either can't be extracted correctly or is in a wrong format (e.g. a non-standard jpg like one using a cmyk colorspace etc.). – Thomas Jan 16 '17 at 14:45
  • So, in other words, `loader.getResource(…)` *does* return a `URL` and you changed it to `loader.getClass().getResource(…)` for no apparent reason and *then* ask us what’s so “magic about a folder”, when an entirely different code doesn’t do what the other does? – Holger Jan 17 '17 at 11:52

1 Answers1

0

In case someone else runs into this I finally figured it out. I needed to use a byte stream for the logo.

ClassLoader loader=Thread.currentThread().getContextClassLoader();
logo = new JLabel(new ImageIcon(ImageIO.read(loader.getResourceAsStream("KCBSEvents/Images/KCBSLogo.jpg"))));

It had nothing to do with the folder in which the jpg file was located.

Wt Riker
  • 514
  • 12
  • 24