• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

images in jar file

 
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?

 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for .jar images look into
getResource(...)
 
naveen yadav
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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.
 
Sheriff
Posts: 28333
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 28333
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The images folder should be put inside your JAR file relative to the class file.
 
naveen yadav
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the package structure inside the JAR file still present? You can use any ZIP program to check.
 
naveen yadav
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Rob.
At last it is working. thanks for your constant guidance


 
Rob Spoor
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
I brought this back from the farm where they grow the tiny ads:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic