开发者

Netbeans creating a dist jar with all images etc included

开发者 https://www.devze.com 2023-01-04 09:52 出处:网络
I am trying to distribute a netbeans project however the jar it creates and the contents of the dist folder are dependant 开发者_如何学Goon some image files which i included into the project - however

I am trying to distribute a netbeans project however the jar it creates and the contents of the dist folder are dependant 开发者_如何学Goon 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


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"...


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.


You can add images the same way:

final Image image0 = Toolkit.getDefaultToolkit().getImage(getClass().getResource("images/1.png"));
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号