I'm working on a project that has several CLASS files and PNG files (for icons). I want to be able to send the BETA version of this project to others efficiently. Obviously this means I need to put everything into a JAR file. I'm using BlueJ (an IDE) to create a JAR file in which I add the CLASS and PNG files (because without the icons the program wouldn't work properly).
When I launch the JAR file the project launches fine but the it doesn't recognize the Icon files in the JAR file, so it doesn't load them (which basically makes the project totally useless! )
I know I have to change something in the code, but I don't know what to put exactly. This is the code of the class containing the ImageIcon objects (which most of the other classes in the project use). I've tried adding ".\NameOfJarFile.jar\" before the filename. But that didn't work.
Firstly, Campbell mentioned I should put / instead of \ to maintain platform independence. But how could it maintain platform independence when I Windows the backslash \ is used :?: (I know Linux and MacOS use / but Windows is different, would a / still work in Windows?)
Secondly I have no idea what the method Icons.class.getResource(String) does or where it comes from. Could you please briefly explain it to me or give me a link to it's API.
Regardless to that I still tried what Paul suggested and it worked fine when I ran it directly from the IDE. But after compressing the classes and image files into a JAR file the compiler gave me this error:
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
... 4 more
I did recompile the classes and I checked that the PNG files are in the JAR file, so that's not the issue.
Still, I don't understand what the method Icons.class.getResource(String) does or where it comes from and I don't see why a / works whilst a \\ doesn't (even though I'm in Windows). I would appreciate any clarifications. Thank you
But when I check the Java API for the method class.getResource(String name) it says "Finds a resource with a given name" and returns a URL. So basically if I'm getting this right the method gets a resource of the class and appends the String name. I don't quite understand what they mean by "resource" though. It is something like File Address / Location? :?