Results 1 to 6 of 6
  1. #1
    bleah's Avatar
    bleah is offline Member
    Join Date
    May 2010
    Posts
    13
    Rep Power
    0

    Default Including image files in runnable jar

    I want to export a runnable jar file from eclipse but also I want to include an image file in jar package. There is no such option to include extra files in eclipse's export runnable jar function.

    But there is an option in export jar function. But when I use this function, I have my desired jar but it is not runnable this time.

    How can I export my runnable jar with extra files included? I don't want to mess with command-line. How can I do this with eclipse?

    I googled for this problem and no one has a proper answer for this. There was a such topic in this forum but looks like no one has a solution, maybe some people misunderstood the problem.

  2. #2
    mniz13 is offline Member
    Join Date
    Jun 2011
    Location
    Boston, MA
    Posts
    7
    Rep Power
    0

    Default Same Issue

    I'm having this same problem. When I run the jar file in my computer this is not an issue as the media is within the same folder, but I need to run this file remotely from a VBScript. This will not pick up the image files I need. Is there any way to include the image files in the executable jar??

  3. #3
    mniz13 is offline Member
    Join Date
    Jun 2011
    Location
    Boston, MA
    Posts
    7
    Rep Power
    0

    Default

    Okay from various online sources and a lot of trial and error I made it work. First you have to change the way you access the files to:

    ImageIcon icon = new ImageIcon(getClass().getResource("/image.jpg"));

    (by the way the getClass() method can't be used in a static method so if you have it in your main you need to move it to another non-static method and call that method in the main)

    Then you need to move the image files to the bin directory and then export to jar file. The files should all be in there and load correctly.

  4. #4
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    There is no such option to include extra files in eclipse's export runnable jar function.
    I've no experience with the build-in jar-export except in simple cases, but there's a very good support for using ant-scripts (build.xml files) in eclipse. (An alternative is maven, but I don't know that either.)
    In an ant-script you can specify whatever you want included in your jar, and which tasks to run in which order, grouped togeter in targets. (Things like showing the used paths, cleaning, copying, compiling, making jar and running.)
    Just google some of the words I used to find out more.

    Edit: In the export jar-menu you can include resources, thus chances are that you don't even need ant. But when projects grow, ant is a must.
    Last edited by Jodokus; 06-17-2011 at 06:53 PM.
    No bug ever had to calculate its fitnessfunction.

  5. #5
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    You need to make the files part of your project. Then they'll be included in the jars automatically.

    The project I'm working on now has a folder called "resource" directly under the "src" folder. Those files are loaded in my program using the method mniz13 described. When I export a jar -- runnable or not -- I do not have to explicitly tell it to include those files. In fact, it doesn't even give me an option. The resource folder is automatically included in the jar.
    Get in the habit of using standard Java naming conventions!

  6. #6
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    kjkrum, you're right. In the meantime I've done the same instead of by ant, just to try it once again, and it worked fine (esp. the runnable version). But I have also programs composed of multiple projects, and then ant is easier: the buildfile grows with your program.
    In fact, it doesn't even give me an option.
    The runnable version asks a "launch configuration", and then it includes all referenced projects. The jar also appears to contain files that are not used in the program, so ant gives a cleaner jarfile.
    No bug ever had to calculate its fitnessfunction.

Similar Threads

  1. Replies: 0
    Last Post: 04-15-2010, 11:06 AM
  2. Replies: 7
    Last Post: 01-11-2010, 10:14 PM
  3. Create and Read Files with name including special characters (, , ,)
    By joseluisbz in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-04-2010, 12:52 AM
  4. Replies: 6
    Last Post: 08-06-2008, 02:41 PM
  5. Text and image files within jar files
    By erhart in forum Advanced Java
    Replies: 8
    Last Post: 01-19-2008, 05:43 AM

Tags for this Thread

Posting Permissions

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