Loading image by relative path name


Question

I have a typical problem which concerns a loading files from specific directory.

This is how looks like a tree of my project:

http://i.stack.imgur.com/Hbnkb.png

And it is a part of code which causes a problem:

ImageView iv = new ImageView(new Image(Main.class.getResourceAsStream(
            "images/image.png"))
            );

Could anyone tell me how to load a 'image.png' picture and where is an error in this code?

1
0
6/1/2013 9:14:36 PM

Accepted Answer

You need to start from the root of your jar with "/". What you are currently doing is getting it from a relative location (from the Main class location).

ImageView iv = new ImageView(new Image(Main.class.getResourceAsStream(
        "/images/image.png")));
2
6/1/2013 9:15:26 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon