This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
naveen yadav wrote:ImageIcon image = (new ImageIcon(getClass().getResource("images/dice.gif")));
Is it fine ?
You don't get the resource from the class for no reason. Your class appears to be in the "abc" package. You chose to use a relative resource name rather than an absolute resource name, so it's relative to the class's package. So it's fine if you put that "images" folder under the "abc" folder in your jar. Otherwise, not.
naveen yadav wrote:why getClass().getResource(path) returning null Although path is "E:\codes\images\animate.gif" ?
Are you kidding? All through this thread you've been saying that the image is in a jar, and now you're turning around and saying it's in a file after all? That's just ridiculous.
the reason for "turning around" is
at start things were working fine with java command But when i a create a jar file and execute it .It was throwing error.
when i edited the code i had to make sure that things are all right with java command first otherwise there is no point in discussing the jar execution
Rob Spoor wrote:The images folder should be put inside your JAR file relative to the class file.
after putting the images folder inside abc(package)
1. invoking java abc.GuiClient is working fine (displaying all the images)
2. created a jar file using cmd
jar cmf header.txt gui.jar abc/*.class abc/images/*.gif and executing jar .
Not working fine does not displaying any images
Why are you using a File to get the path? Just use ImageIcon img2 = new GuiClient().getImage("images/animate.gif");. Also make sure that the case is exactly the same; Windows may allow you to use a different case, but resources are case sensitive. images/animate.gif is not the same as images/Animate.gif.