• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Include images to jar

 
Ranch Hand
Posts: 87
1
Netbeans IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a lot images, that my program uses. How can I include them to the builded .jar file?
I do not want to copy also the folder of the images with my program.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can just bundle the images inside the .jar file and retrieve them as classpath resources. There are several ways to go about this, and which one fits your situation best is up to you. You can include all the image files directly at the root of the .jar file or in a specific resource subfolder, or you can place them directly next to .class file in a specific package. The location inside the .jar file and the means by which you can retrieve the images in your application are linked, though.

For instance, say you have a class MoosePrinter in a package com.javaranch, which displays moose.gif - an image of the JavaRanch mascot. Inside your .jar file you'd have a folder/subfolder nl/javaranch, which contains two files MoosePrinter.class and moose.gif. In the code of MoosePrinter you can retrieve the moose.gif image as a classpath resource by calling Class.getResourceAsStream("moose.gif"). This approach would only work from inside the MoosePrinter class or any other class in the com.javaranch package, though. Let's say you'd placed the moose.gif image in the root of the .jar file - the previous approach from retrieving the image from inside the MoosePrinter class wouldn't work. You would have to use ClassLoader.getResourceAsStream("moose.gif"); instead. Notice this is a method of ClassLoader, whereas the previous approach used a method of Class.

So, there are several ways to bundle the images and several ways to get at them from inside the application, depending on where you bundled them. Which approach you pick is up to you, but it's good to be aware of the methods that allow you to load resources on the classpath, the most commonly used are:

ClassLoader.getResourceAsStream()
ClassLoader.getResource()
Class.getResourceAsStream()
Class.getResource()

Note that these all behave differently and have different return values, so read the API documentation carefully!
 
Kovacs Akos
Ranch Hand
Posts: 87
1
Netbeans IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My image files are stored in a separated folder beside the .java and .form files and I am already using relative paths in my program to use images with ImageIcon.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm afraid you'll have to change you code slightly, because that won't work if you bundle both code and images inside a .jar file. You'd need to use one of the methods I mentioned for loading classpath resources. There are several that will return a URL, which you can then pass to the ImageIcon constructor.
 
We don't have time to be charming! Quick, read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic