Faris Rehman
Faris Rehman

Reputation: 129

Get resource in Java

I'm trying to set the icon image of my JFrame. I can't figure out how to get the image file as a resource to be loaded using: Toolkit.getDefaultToolkit().getImage(URL);

I've heard of Class#getResource() but it won't work in my case as my file structure is:

com.faris.test.Test.class
com.faris.test.Test2.class
res.icon.png

My Test.class is the class that extends JFrame and is trying to fetch "icon.png", but I'm not sure how to get it as my class (Test.class) is in another folder. Any solutions to this?

Upvotes: 0

Views: 604

Answers (1)

nulldev
nulldev

Reputation: 615

It doesn't matter what folder your class is in. You can just use: Test.class.getResource("/res/icon.png");. Make sure to include the '/' at the beginning of the path.

Have you actually tried this?

Upvotes: 2

Related Questions