Java – access jar resources

I have a jar file and resources (mainly configuration cache, log, etc.) that I want to distribute

I have a problem with the relative path of these resources, so I found it in another stackoverflow problem, which shows that this is an effective method:

ClassInTheSamePackageOfTheResource.class.getResourceAsStream('resource.xml');

Sadly, that won't work

Any ideas? thank you!

Solution

Make sure your resource folder is listed as the source folder in the project settings Also, make sure that the resource folder is set to export when you build your jar

You can put the Add the zip extension to your jar file and open it to ensure that your resources are included in the expected location

I always use this absolute path:

InputStream input = this.getClass().getResourceAsStream("/image.gif");

When absolute paths are used, "/" is the root folder in the jar file, not the root folder of the host

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>