my dog learned polymorphism*
The moose likes Java in General and the fly likes images in jar file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "images in jar file" Watch "images in jar file" New topic
Author

images in jar file

naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384


hi ranchers ,

i have a packaged a small application of swing into a jar file called gui.jar suing following jar command

cmd>jar cmf manifest.txt gui.jar abc/GuiClinet.class images/*.jpg

content of mainfest.txt
Main-Class: abc.GuiClient

i have used some images for various labels like


but when double click gui.jar file in some other directory it is not showing the images of labels ?
note images is a directory under which all images are placed.


how can i resolve that ?

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
for .jar images look into
getResource(...)
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

Michael Dunn wrote:for .jar images look into
getResource(...)


ImageIcon image = (new ImageIcon(getClass().getResource("images/dice.gif")));

Is it fine ?
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

calling
ImageIcon img3 = new GuiClient().getImage("images/animate2.gif");


}

when creating a jar it throws error : Couldn't find fiel images/animate2.gif
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> when creating a jar it throws error : Couldn't find fiel images/animate2.gif

if it works OK in the IDE, check the capitalization of the folder and the filename.

e.g. animate2.gif might actually be Animate2.gif, which will work in IDE's, but not jars.

if using windows, and the files are OK in windows explorer, open the command prompt
and burrow down to both folder and file names to check/confirm the capitalization.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

Now i am passing the absolute path of the file.





when i running using java command java abc.GuiClient

why getClass().getResource(path) returning null Although path is "E:\codes\images\animate.gif" ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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.
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

Paul Clapham wrote:
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.
But
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




naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384


when code is changed to


and run using java abc.GuiClient : it run fine displaying all the pictures.

but when jar file is created (jar cmf header.txt gui.jar abc/*.class images/*.gif) and run using java -jar gui.jar
output is couldn't file : images/animate.gif


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

The images folder should be put inside your JAR file relative to the class file.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

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

here is a code


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Is the package structure inside the JAR file still present? You can use any ZIP program to check.
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

Rob Spoor wrote:Is the package structure inside the JAR file still present? You can use any ZIP program to check.


yes package structure abc do present inside jar file


the above code prints all classes inside abc/className.class And all images inside abc/images/imageName.gif
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

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.
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

thanks Rob.
At last it is working. thanks for your constant guidance


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: images in jar file