my very first question:
I've been trying to figure this out for a couple of days, but I got to the point I lost my patience.
The following are some code and my project structure.
QUESTION: how can I get getResources() to work in eclipse and after being imported to jar?
Thanks for the help.
public enum Icons {
XXX("src/resoruces/icons/xyz.png");
private ImageIcon icon;
Icons(String path) {
try {
// will fail miserably in eclipse and after exporting to jar
URL imageURL = getClass().getClassLoader().getResource(path);
icon = new ImageIcon(imageURL);
} catch (Exception e) {
// works like a char in eclipse and after creating the jar file
// with the files in the same directory
System.out.println("getResoruce() did not work");
icon = new ImageIcon(path);
}
}
解决方案
If the png files have been packaged into the JAR in the same locations as they are in the original src directory then
XXX("resources/icons/xyz.png");
should produce the right result.