# How to locate assets on different computers? JavaFX

i've programmed a little game in javafx in Eclipse. It uses 3 file-path url's to locate the images, sounds and savefile folders. My problem is that those url's have been hardcoded and therefor obviously only work on my computer. Like this:

    public static String imageDir = "FILE:///D:/Users/tlins/eclipse-workspace/SpaceWars3.0/src/images/";


I want to export my game in a jar file and then the programm should figure out for itself where the assets are located. I originally assumed that it would start looking in the same folder as the main class, and that i could just type

public static String imageDir = "images/";


But it gave me an exception error "url not found" regardless.

So im just here to ask how you people normally do this? This can't be an uncommon thing right?

Should i somehow let the program figure out the EXACT path like shown (far) above? Or is there a way to inform it that it should start looking from the folder of main class?

• – DMGregory Apr 28 '18 at 15:12
• @DMGregory I'm sorry, i just don't understand alot of what people say on there. In case i wasn't clear enough: I want to run my programm on different computers, but i don't know how i should let the computer know the correct filepath to the asset folders on every computer. – Thomas Linssen Apr 28 '18 at 15:22

In order for your assets to be able to load from the same directory on any computer you have to use relative folder structure. Create an assets folder called res and make sure its part of your build path.

Inside that folder you can place your assets in sub folders. Ex: res/images/player.png

Now to load the following assets you need to use the getResources() function. However if you want this to also work inside a .jar file this is the best solution:

URL assetLocation = Assets.class.getClassLoader().getResources("images/player.png")


Then you can use that URL as you please.

Link to Java Docs:

i have been using getResource, this works even if you are running theyourgame.jar.

1.- i created a res folder and i put there all my images, 2.- i loaded all those images with ImageIO.read(this.getClass().getResource( "/demo1/xpace/ene1.png" ) )

image path its: res/demo1/xpace/ene1.png