Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Images in .jar File

  1. #1
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Images in .jar File

    I have a program which uses images. It works fine when I execute the program, but when I clean+build and run the jar file - no images.

    I added an images folder. The pics are in it: "...Documents\NetBeansProjects\Flip Game\src\flip\game\pics".

    How can I bundle the images in the jar file?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Images in .jar File

    The images are likely already in the jar file. Have you used an unzip program such as 7-zip to check it? The key will be how to access them. Remember that files don't really exist inside of a jar file, and so you'll need to access the images as resources using a path that is relative to the class files.

  3. #3
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: Images in .jar File

    Quote Originally Posted by Fubarable View Post
    Remember that files don't really exist inside of a jar file, and so you'll need to access the images as resources using a path that is relative to the class files.
    You're right, they are there. How do I access them?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Images in .jar File

    I've usually used the ImageIO class's static read(...) method. It can take an InputStream, and you can get one from your class or class loader and use that. For instance, if you're in non-static land, for instance inside of your class's constructor, you could try something like

    Java Code:
    try {
       BufferedImage img = ImageIO.read(getClass().getResourceAsStream("flip\game\pics\myImage.jpg"));
    } catch (SomeExceptionIForgotWhat e) {
       e.printStackTrace();
    }

  5. #5
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: Images in .jar File

    Sorry but I'm very new to Java (and programming in general) and I don't understand what you said. What method(s) should I use and where exactly should I use them?

    Thanks much

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Images in .jar File

    It's hard to say without knowing more about your code, your problem, the structure of your jar file....
    You will want to experiment with your code of course -- it's hard to break your computer by doing this, and if that fails, then show us more and tell us more.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Images in .jar File

    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 0
    Last Post: 11-14-2012, 11:15 AM
  2. Replies: 9
    Last Post: 07-28-2010, 11:27 AM
  3. How do I store images in a zip file in Java
    By mglover in forum Advanced Java
    Replies: 1
    Last Post: 06-11-2010, 03:53 PM
  4. How to add images to a .jar file?
    By dunafrothint in forum New To Java
    Replies: 3
    Last Post: 03-05-2010, 07:38 AM
  5. Images in JAR File (via Eclipse)
    By AndrewM16921 in forum New To Java
    Replies: 13
    Last Post: 10-08-2009, 06:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •