views:

24

answers:

2

Hey!

I am trying to distribute a netbeans project however the jar it creates and the contents of the dist folder are dependant on some image files which i included into the project - however these images are not in the dist folder and I cannot workout how to make things work so I can export the project in a distributable format including all the things it needs.

Can somebody please tell me how I can export a project which runs within Netbeans without using the project's /dist folder which includes everything it needs?

Cheers

Andy

A: 

One way to achieve this is to add a folder (f.i."resources") in your project's src dir. Then copy the images to that dir. Now the images should get included when you build the project (if I remember correctly). Accessing the files can be accomplished with "getResourceAsStream"...

Emil H
Ok So tried this, when running from netbeans this code works fine:[code] URL imgOn = cldr.getResource("resources\\on.png"); URL imgOff = cldr.getResource("resources\\off.png");ImageIcon on;ImageIcon off; public void run() { System.out.println("URL:"+imgOn); on = new ImageIcon(imgOn); off= new ImageIcon(imgOff);...[/code]When running from the jar in the dist this returns URL:null.....
RenegadeAndy
A: 

If whatever resources you are interested in are in the classpath, packaged in the jar, war, or the distribution, you can retrieve them by getting resources.

The convention is indeed to have a directory named 'src/resources' that serves as the root for this. Depending on the amount and scope of the resources you are using you may also want to add a sub-directory hierarchy to keep the organization and state of the resources manageable.

Also, not that a resource can be any file, an image, sound, text, xml, binary, etc. no limitation.

Finally, the call will look like this if you are using an object method: getClass().getResourceAsStream("resources/myResource") - or - getClass().getResource("resources/myResource") depends on if you want a stream or just the URI at that point in the code. Typically one would use the URI for delegating the processing of the resource elsewhere and the stream form when you are processing it in-line.

For a class method, you will need to do something more like:

new Object().getClass()...

The think to keep in mind here, is eventually this is resolving to the class loader and it is from that class path that the resource will be fetched.

JiroDan