Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should my resource folder be with a runnable jar?

Tags:

java

resources

In eclipse, i have a project i am working on with resources that i call like this for example

URL url = FighterComponent.class.getResource("/gfx/characters/");
File file = new File(url.getPath() + info.name + "/" + frame.path);

/gfx/ being a folder inside of res. res is my resource folder which i have linked by doing this:

run > run configurations... > Classpath > Select user entries > Advanced > Add folder

My question now is that where should the res folder be after i exported my project into a runnable jar for my friends to be able to test it? I tried putting it in the same folder as my jar and inside my jar and for both the URL was null.

Any idea ?

like image 584
Nicolas Martel Avatar asked Dec 27 '25 16:12

Nicolas Martel


1 Answers

If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.

So, you should have a gfx/characters directory inside the jar file. Under this directory, if you have an image.jpg resource, you will have to retrieve it using:

URL url = TheClass.class.getResource("/gfx/characters/image.jpg");

You can also use the getResourceAsStream() to get an InputStream that you can read from.

like image 192
Dan D. Avatar answered Dec 31 '25 18:12

Dan D.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!