I have 1 package that manages the main JFrame of my application.
This JFrame needs to access an image (which is a resource) of another package.

I was able to get it to work using:

new ImageIcon(getClass().getResource("../"+packageName+"/icon.jpg"));

But this does not work when using a runnable jar (getClass().getResource("../"+packageName+"/icon.jpg")) returns null). Any suggestions?

Recommended Answers

All 3 Replies

I know all that already, but it doesn't explain how I can get the image from a different package in the same project...

getClass().getResource(...)

package OtherApp:
icon.jpg


package MainFrame:
MainApp - needs icon.jpg


You would think
getClass().getResource("../OtherApp/icon.jpg") would work.

It does, but not when using a runnable jar.

Has the directory structure of the packages been modified after the files have been added to a JAR? You will need to modify the

getClass().getResource("../OtherApp/icon.jpg")

statement to reflect this modification. Also, did you read the section regarding URL's in that link I sent you just below

Accessing resources in a JAR.

?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.