(I've attempted to put the tl;dr stuff in bold)Hey everyone, I'm totally new here and relatively new to Java, I was hoping I could get some help regarding a practice game I'm making.I did a few searches, I didn't find anything like this, but I also wasn't going to spend my entire evening tunneling through forum searches XD

Anyway, my game was working fine when I ran it in Eclipse, but I wanted to be able to show it to my buddies, I exported it as an executable Jar and ran it.Nothing ran at all. So I looked into it and apparently my images aren't loading up properly when I run the Jar.

Why is it that Eclipse can find my images and run the thing fine and when I export it nothing works?I'm assuming it's to do with where I placed my files. I HAVE NO IDEA WHERE TO PUT THEM! But I do want them in an images folder at the very least.I've currently got them in "src/platformer/images"Platformer is my package name/folder and has all my code in there.

The code I'm using to load the images up has changed a few times, but what I'm currently using is:

Now I'm not entirely sure what I'm doing there, I just read from Kev Glass' tutorials that it's a good idea to do it that way.Can anyone lend some insight into this? Do I need to move my images folder somewhere? Do I need to add a slash or something to my file path? I've spent all day on this annoying problem and haven't got anywhere! I know it's a simple, stupid problem, but please bear with me!

Thanks in advance

P.S. You may need to dumb down your answers a little bit, I'm not a full on programmer yet! So many things I've looked up today where people had problems that were solved thusly, 'Simply do this! *insert a whole mess of uncommented code with poorly named variables*' 'Wow thanks for solving my problem! '. Then you have me sitting here, thinking in angry scribbles and reaching for another coffee...

That's starting to sound like the solution to my problem!See I only just switched over to Eclipse yesterday, I've been trying to learn it as best I can, yet a little thing like this seems to have slipped under my radar.So the properties where I assign this as a resource folder, is that the properties from the project menu? Or is it the properties from right clicking on the folder itself? Also, whereabouts in properties? Is it build path or linked resources or something I've missed entirely?

I used that code you suggested, it worked in Eclipse, but after exporting is an executable Jar, still didn't work.I seriously think something is wrong with my folders or something. Am I correct in assuming that the images folder should be a source folder in the Java Build Path in properties? Because I've tried that too, and it broke it again I'm definately doing SOMETHING wrong though, since I've looked through tons of videos today and they just breezed through it...

EDIT: Here's a picture of my little project list that might shed some light on the subject. I'm not sure that images folder is in the right place, I've moved it around a fair bit though....

When using Class.getResource, you must know that it is relative to the Class. The easiest way to get a resource is to put a forward slash at the beginning of the path. This signifies the root of the jar file when in a JAR file or the root of your project (the src folder). In this situation, remove the "platformer.images" from the build path. To load any file, use ImageIO.read(Class.getResourceAsStream("/platformer/images/MyFile.png"));

EDIT: There is no difference between using Class and the Thread.currentThread().getContextClassLoader() way.

EDIT2: In your project image, that white colored "package icon" means there are no files under that folder....

Use getClass().getResourceAsStream(). This gives you a reference relative to the current class, using its classloader, which is much more likely to do what you want. This doesn't matter much for the average toy app, but if you ever write web apps or god forbid mess with OSGi, you'll absolutely hose yourself if you start grabbing random classloaders or try to use the boot one.

Use getClass().getResourceAsStream(). This gives you a reference relative to the current class, using its classloader, which is much more likely to do what you want. This doesn't matter much for the average toy app, but if you ever write web apps or god forbid mess with OSGi, you'll absolutely hose yourself if you start grabbing random classloaders or try to use the boot one.

It's a rite of passage to work on an "enterprise" application and bang your head against the wall tinkering with classloaders.

At this point, I'm not even sure what exactly a class loader is, I just read that if I do it this kind of way it's better .I certainly appreciate all the time you guys are putting into helping me out, I've done what I think you guys were telling me to do, it looks like this now:

However, it's not even running in Eclipse anymore... So let me get this straight, the src folder is treated as the root directory right? So surely putting the images folder in src folder would mean that the path, "/images/myfile.png" would work right? ...Cause it's not. I really think the problem is WHERE I'm putting my images folder, I simply just don't know the correct place for it.This is such a rediculous error for me to be stuck on...

This is starting to come together, thanks guys, it's good to see what it's supposed to look like haha! I was getting quite confused...In any case, I remade the images folder from scratch and made sure it wasn't in the build path or anything, just a plain folder. I put laid it out the you have yours there Ra4king. Good news! It now runs in Eclipse!Bad news... it still won't run as an executable jar...

Oh my goodness! I can't believe I did that... Clearly not enough coffee!Well it WORKS now! Thanks a lot to everyone who took the time to help out an Eclipse newbie like me! ...Now I can resume actually coding the thing..

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org