Fat Uncle D
Fat Uncle D

Reputation: 19

What is the path to my .png in my project (java eclipse)?

I am trying to understand how java paths work in eclipse but I am terribly confused. I have an image troll.png and I am using ImageIO.read(getClass().getResourceAsStream("troll.png")) to access it.

Now, if I put troll.png in the same package as the classes I use it in, it works. But I want to place it in another folder, so I made a folder resources in the project, parallel to the src folder where I keep my classes.

How can I read an image from that folder? I've tried every path I could think of and none worked.

Upvotes: 1

Views: 533

Answers (2)

s2lab
s2lab

Reputation: 1

Issue can be solved as following:

ImageIO.read(getClass().getClassLoader().getResourceAsStream("troll.png"));

Please see also: getResourceAsStream returns null

Upvotes: 0

ninja.coder
ninja.coder

Reputation: 9648

I made a folder 'resources' in the project, parallel to the 'src' folder where I keep my classes. How can I read an image from that folder?

Prefix the path with / in order to access the resources in the root directory. If you are not doing it, then you are just looking at package level, relative to the current package.

Here you go:

ImageIO.read(getClass().getResourceAsStream("/troll.png"));

Upvotes: 2

Related Questions