I have been trying to get image resources to display on a GUI I am developing, but am having a difficult time getting them to display. I've come across other questions about loading resources such as java-swing-displaying-images-from-within-a-jar and images-will-not-work-in-a-jar-file, but they aren't working for me.
My issue appears to be the same as the first link, where images appear when run from Eclipse and don't appear when run from the command line using Jars. However the solution to those questions don't make the images appear.
The code I have for retrieving resources is:
public class R {
public static final String resourcePackage = "path/to/image/package";
/**
* Returns the resource string that refers to the <code>resource</code> file
* in the <code>path.to.image.package.png</code> package.
*
* @param resource
* the file in the <code>png</code> package
* @return the full resource string
*/
public static String png(String resource) {
return String.format("%s/png/%s", resourcePackage, resource);
}
public static ResizableIcon resizableIcon(String resource) {
return ImageWrapperResizableIcon.getIcon(R.class.getClassLoader()
.getResourceAsStream(resource), new Dimension(48, 48));
}
}
I call it when generating the GUI
JCommandButton connect = new JCommandButton(i18ln.getString("ports"),
R.resizableIcon(R.png("serial-port-32x32.png")));
A print statement indicates that the resource was found because R.class.getClassLoader().getResourceAsStream
returns an instance of sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream
.
I'm stumped. I have spent hours trying to figure this out, but could use some help. Any ideas?
FYI: I don't think it matters, but I am using Flamingo for my GUI.
EDIT: per Stefan's request
src/ main/ java/ com.company.project (packages) R.java MyGui.java resources/ com.company.project (packages) .png (package) serial-port-32x32.png (more images) .i18ln (package) MyGui.properties
As for more code, I don't know what else I can provide that will be of much benefit for this question. All the code for retrieving resources and how I use that code is provided above. Was there something specific you were looking for?
UPDATE:
When I create a Jar using Eclipse and run it from the command line, the image resources display properly. When I create a Jar using Gradle, the images are not displayed. So there is something being done differently when generating the Jars that allows images resources to be accessed properly via the Eclipse Jar, but not the Gradle Jar. I opened a question on the Gradle forums with respect to this issue.