2

This is a really common error, because there are tons of threads about it, but I'm not sure if since my situation is slightly different from all of them the solutions don't work?

Basically, I'm in eclipse. I have a source folder called src, then I have a package that goes down three folders, then the class in question. The class uses the code:

BufferedImage im = ImageIO.read(Thread.currentThread().getContextClassLoader().getResourceAsStream(filenames[x].concat(extension)));

surrounded by a try/catch. filenames is an array of all the file names I am loading (this code is run multiple times in a for loop) and extension is ".PNG". The pictures are located in another source folder called EngineTextures.

Running this program works fine in Eclipse! All textures are loaded and all my other code runs! However, I export it as a runnable jar and run it in command prompt to recieve input == null errors on all of them, pointing to the line that has ImageIO.read(Thread.currentThread() in it.

The kicker is this whole thing worked in a separate project before, and when I even tried re-exporting that project, I recieve the same errors on completely unchanged code. This leads me to believe I have some obscure Eclipse setting changed wrongly.

Opening the jar, my MANIFEST.MF has a version of 1.0 and a classpath of just plain ., which I thought was correct for this kind of thing? The Main-Class points to the right place, and all my pictures are right there next to the META-INF folder.

Solutions I've looked at unsuccessfully:

getResourceAsStream working in eclipse, but not when run as applet in browser

Why does getResourceAsStream() work in the IDE but not the JAR?

Java IDE - Eclipse, Importing resources

Audio file in jar made by Eclipse IDE

getResourceAsStream() returning null in jar but fine in eclipse

Additionally, I completely deleted the workspace and recopied my pictures and code into the same state, thinking maybe some .metadata thing was wrong, to no avail.

Thank you, in advance, for any and all help. I hate to make a repeat like this but no solutions have worked thus far. Please let me know if I have not given any crucial information.

Community
  • 1
  • 1
snickers10m
  • 1,709
  • 12
  • 28
  • How do you specify the file names? If you put a log statement what would be the result of `filenames[x].concat(extension)`? – c.s. Aug 05 '13 at 22:36
  • This code is in a method `loadTextureSet(String[] f)` and the extension is a static final of the class. Therefore my main calls it with a String[] that is set sorta like: `String s = { "Button", "Background" };"` etc. – snickers10m Aug 06 '13 at 00:10
  • As I said, it's a for loop, so if i set `String[] f = { "Texture A", "Texture B", "Texture C", "Texture D" }` then a println would put out `Texture A.PNG` `Texture B.PNG` `Texture C.PNG` `Texture D.PNG`. – snickers10m Aug 06 '13 at 02:00
  • _The pictures are located in another source folder called EngineTextures_ Do the images get to the root package of the same jar? If yes then `getResourceAsStream("Button.png")` works just fine – c.s. Aug 06 '13 at 06:20
  • Your images should be packaged in the jar (same or different does not matter as long as they are in the default package using the above code). If you are running from a jar and trying to load images from a file system, then this would never work as is stated in @EJP 's answer – c.s. Aug 06 '13 at 06:29

1 Answers1

1

Opening the jar, my MANIFEST.MF has a version of 1.0 and a classpath of just plain ., which I thought was correct for this kind of thing?

No. The Class-Path entry in a JAR file names other JAR files, relative to the location of this jar file. It doesn't name directories:

"The value of this attribute specifies the relative URLs of the extensions or libraries that this application or extension needs."

That in turn implies that resources to be loaded via getResourceAsStream() must be in JAR files.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thank you for your research. In that case, if I package up my pictures in another jar file in the same folder, to my understanding of this that would work then? Doing so does not, in my tests just now. Is there any other way to load image files that are more effective then? A way that would work in both jar format and eclipse? I would prefer to access the images from the same jar the code is running in. – snickers10m Aug 06 '13 at 03:43
  • What stumps me is this method worked in a different project before... I literally copied `getResourceAsStream()` and the rest of it from that project, and now even that project being exported doesn't work. – snickers10m Aug 06 '13 at 03:48
  • You need to fix your Class-Path to name the other JAR files. – user207421 Aug 06 '13 at 03:52
  • Working on fixing that, but shouldn't a plain `.` in the classpath mean that it accesses the pictures from the same jar file? Not sure why it doesn't do that. – snickers10m Aug 06 '13 at 04:23
  • I've already addressed that in the first paragraph of my answer: "No." – user207421 Aug 06 '13 at 04:26