1

I wanted to know where should I keep my resources like I am displaying some images, playing some media files while making executable jar? Should I include it in executable jar or I should keep outside the executable jar?

If I keep the resources outside the jar what URL location URL should I pass in my program to access the images?

Actually the problem is I want to make distributable copy of my jar file.

If I give location of my local system for accessing the images and media files it will work in my system but what when I distribute it in other systems?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Amaan
  • 9
  • 7

3 Answers3

4

It depends on your application.

You can pack resources into the jar. Typically it is good for resources that are never changed: company logo, icons etc. You can read them using getClass().getResourceAsStream().

Other solution is to download the files from server (e.g. over HTTP). This solution is good for media that you do not want to pack together with application. For example video clip you want to play to user. Or, probably localized icons from the previous example or localized messages for multi-lingual applications.

The resources that you are downloading can be cached. You can use User's temporary directory (System.getProperty("java.io.tmpdir")) or sometimes using preferences API.

AlexR
  • 114,158
  • 16
  • 130
  • 208
3

This isn't a radical answer but you could, in essence, create a resources project for images, properties and the like (media even).

You can then add your resources project (henceforth referred to as "MyProjectWsResources") as a child to your code project (henceforth referred to as "MyProjectWsClient"). Since MyProjectWsResources is in MyProjectWsClient's build path, it makes referencing your resources easier. If you're unclear of what putting something in your project's build path entails, it's saying anything in MyProjectWsResources's src folder is in MyProjectWsClient when it goes to referencing for information (as long as you're not using absolute paths :))

Why go for the approach of multiple projects? IMO, it separates code from resources so you download resources and code separately and your clients need not download the resources projects repeatedly when there are updates to your code only (I feel updates to code are far more frequent as compared to updates to resources). Lesser server bandwidth (important if you're using Java WS or any other packaging/system which I now realize you probably aren't).. still, hope this helps :)

javatarz
  • 1,174
  • 16
  • 30
  • Sir i am not using any online updates or something its just an java desktop application where a CD/DVD is given to the user ..... Could u please elaborate the method of making another project of resources referred as MyProjectWSClient – Amaan Sep 09 '12 at 14:15
  • Sir i am waiting for the response please provide me the solution – Amaan Sep 09 '12 at 14:31
  • @Amaan: this is his solution. – Dave Sep 09 '12 at 14:37
  • Sir please provide me an example of resources project as mentioned above we can create another child project of resources(for images,media) named as MyProjectWsResources....please help – Amaan Sep 09 '12 at 14:43
  • @Amaan: the concept of a "project" is specific to your particular environment, i.e. Netbeans. Eclipse, etc. – Dave Sep 09 '12 at 14:48
  • I don't know how to create a Resource project in Netbeans and how i can access my images or media files (i mean how can i call the Resource Project from my main application project)... – Amaan Sep 09 '12 at 14:53
  • @Amaan: When I said a "resource project", I meant creating a regular Java project in NetBeans like you already have and then putting your resources inside it. It's just like any other project you create in Netbeans. In case you need help with creating projects in Netbeans, read [this NetBeans help page](http://netbeans.org/kb/docs/java/project-setup.html) – javatarz Sep 09 '12 at 15:27
  • +1 This approach works well with [tag:java-web-start] as a deployment option. – trashgod Sep 09 '12 at 17:14
  • You're right @trashgod, I initially started writing the solution thinking this was a [java-web-start](http://stackoverflow.com/questions/tagged/java-web-start) query as evident from my usage of "WS" in the solution :) – javatarz Sep 09 '12 at 17:32
  • @JAnderton D'oh, I glossed right over it! Let me suggest adding the excellent [JWS](http://stackoverflow.com/q/3687176/230513) tag info link in a future edit. – trashgod Sep 09 '12 at 17:53
2

I want to make distributable copy of my jar file.

Fist I will address the only sentence in your question that was not a question.

A great way to distribute a Swing desktop application to multiple users from the click of a link on the net, is Java Web Start.

Deployment with JWS would mean the resources would need to be in a Jar. For best results with the 'auto updating' nature of JWS, the Jar(s) for media would be:

  • referenced from a separate, sand-boxed, extension so they can be shared with other applications, and loaded/updated separately and lazily (as needed).
  • Compression:
    • uncompressed, for video, sound and image
    • compressed for textual information (HTML, RTF, CSV..)
  • Put in a path in the Jar that is known to the application. (e.g. /resources/video/vidNNN.mp4)

Resources in Jars are an and must be accessed by URL (or InputStream as mentioned by Alex, but URL is more robust). Quoting the info. page.

URL urlToResource = this.getClass().getResource("/path/to/the.resource");

During development, it is generally best to arrange a build that assembles the resources in the same way the end user will get them - to build the app. each run.

In other cases you might want to leave the resources at a public location on the server and access them as needed, but this effectively makes the server necessary for running the media related parts of the app. It seems your resources are both static (user does not change them) and an 'application resource'.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433