aspose file tools*
The moose likes Java in General and the fly likes Packaging an entire application in one JAR file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Packaging an entire application in one JAR file" Watch "Packaging an entire application in one JAR file" New topic
Author

Packaging an entire application in one JAR file

Daniel Searson
Ranch Hand

Joined: Dec 03, 2000
Posts: 83
Howdy folks,

I am currently working on a relatively simple Java application that uses Swing for its GUI. As such, there are several image files that it needs to load for icons etc. There are also a few text files that it might need to read for help files.

Ideally, the entire application, including all of the picture/text files, will be packaged into a single JAR file. At runtime, the application would be able to load the required files out of the JAR file.

Now, I'm not sure if there is a simple solution to this. If there is, please share because my googling hasn't given me much success.

Alternatively, I suppose what the program needs to do is find the JAR file that is being loaded from - this in itself is not an easy task because the working directory may not always be the same as the directory with the JAR in it. After finding the JAR file, I figure I can use the JAR API included with Java to pull the required files out and load them up.

Any help would be greatly appreciated. Thanks.


- Daniel
Arto Pastinen
Ranch Hand

Joined: Dec 13, 2002
Posts: 79
Hi!

Personally i use same method. I use maven and sourceforces, javaapp plugin to build jar. javaapp plugin also extract all dependencies (ext. jars) to final jar.

- Artsi
Daniel Searson
Ranch Hand

Joined: Dec 03, 2000
Posts: 83
Hi Artsi,

Thanks for your reply. I'm not really concerned about packaging up different class dependcies, just about packaging the image files into the JAR as well.

Basically, I don't want users to have to install anything - just download the single JAR from my website and run that.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
They will at least need a JRE to run your Java program, right? And, all modern JREs come with Java WebStart right? And, you will probably be packaging your application as an executable jar, right? Then, why not use WebStart if you're going to be serving the application from a webserver anyway?


James Carman, President<br />Carman Consulting, Inc.
Daniel Searson
Ranch Hand

Joined: Dec 03, 2000
Posts: 83
Thanks for your reply James. It seems that I have found a solution to the problem myself.

Basically the solution I have found is to use the getResource() method in the Class class to retrieve a URL to the file I need. This works whether the image or other required external file is inside the JAR file or not. This is all on

http://www.cs.duke.edu/courses/fall01/cps108/resources/jar.html

Thanks again.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Yes, that's true. You can use the classloader to load resources from the classpath. And, you don't need to know where your jar file is located for that. Sorry, I think I concentrated too much on the "I don't want them to have to install anything" part. It sounds like you're on the right track. Good luck!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Packaging an entire application in one JAR file
 
Similar Threads
how do you load images from a jar file for image buttons?
Reading and Writing Resources
Files
Manifest file woes
Use external properties in the Jar file