Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Packaging an entire application in one JAR file

 
Daniel Searson
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arto Pastinen
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Daniel Searson
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic