Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
NOTE: I hesitated whether to put this topic in the SWING Forum or in this Forum since I'm using the ImageIcon class, put this consists a problem with a JAR file.
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.
You used a constructor whose description says "Creates an ImageIcon from the specified file". But your images aren't in files, they are resources in a jar. Assuming your images are in the root of the jar's directory structure, I would try this:
Ok I realized I made I slight mistake in my post. By ".\NameOfJarFile.jar\" I actually meant that I put ".\\NameOfJarFile.jar\\". There are two things that I don't fully understand.
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.
Oh wait, I just got it working. I didn't realize that I had type Icons.class.getResource("\\0.png") instead of Icons.class.getResource("/0.png") (Which is what Paul suggested). It works when I put a / instead of \\.
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
I think, but I am not certain, that the Windows version of the JVM can translate / to \ in file paths. I don't know, but I don't think it works the other way round. You could easily find out by passing file names like C:/DocumentsAndSettings/Olivier/MyDocuments/myFile.txt and \home\olivier\Documents\myfile.txt on different operating systems and seeing which one works and which produces FileNotFoundExceptions.
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? :?
I will suppress my every urge. But not this shameless plug:
Free, earth friendly heat - from the CodeRanch trailboss