permaculture playing cards*
The moose likes Java in General and the fly likes Creating a Self-Contained Jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Creating a Self-Contained Jar" Watch "Creating a Self-Contained Jar" New topic
Author

Creating a Self-Contained Jar

Gad Flailimbs
Greenhorn

Joined: Mar 21, 2007
Posts: 15
I've looked around and read some stuff but I'm still confused on how to get my Jar file to be self-contained. I'm using Eclipse with separate Source and Class folders. I use about 40 different images while running my program and they're all in different folders.

I've tried:

getClass().getResource("Images/character.gif")

I've also read about some ClassLoader you can use to get a similar effect:

ClassLoader cldr = this.getClass().getClassLoader();
ImageIcon area2 = cldr.getResource("Maps/area2.png");

Neither of these seem to work. I've tried putting all the files in the source and bin folders. Regardless of all of this I still can't seem to get it to work properly. The Jar's just won't function without those folders near them even though I include the folders & files in the jar when I create it.

What do I need to do to get this to work?


Java Game:<br /><a href="http://pokemon.ele-mentalfury.com/" target="_blank" rel="nofollow">http://pokemon.ele-mentalfury.com/</a><br />Webcomic:<br /><a href="http://ele-mentalfury.com/" target="_blank" rel="nofollow">http://ele-mentalfury.com/</a>
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
You'll need to start the path with a slash, as in


Ping & DNS - my free Android networking tools app
Wayan Saryada
Ranch Hand

Joined: Feb 05, 2004
Posts: 105

Hi,

You can also use:



Website: Learn Java by Examples
Gad Flailimbs
Greenhorn

Joined: Mar 21, 2007
Posts: 15
How about if I want to read in a text file from the jar? I've found that some constructors for Readers don't like reading from URLs that getClass().getResource("maps.txt") provide.

Would I use something like the following:
getClass().getResource("maps.txt").getPath(); ?

I think the class I'm using for reading files is the Scanner but I'll have to check on that.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Creating a Self-Contained Jar