"Yet again, I am confused about how java load's it's files."
When loading an embedded-resource, you always want to load from the class path, not by File
. File
will look for resources in the file system, which may work on your file system, but once deployed on a jar, will not work on other systems, and most likely not even yours.
For URLs, what you need to know is where the different ways to obtain a URL will search from. When using a class loader (getClass().getClassLoader().getResource()
)the search will begin from the root, so this path
"res/shader/some_shader.glsl"
will work. On the other hand, when just using getClass().getResource()
without the class loader, the search will begin from the calling class and its package location. That's the the extra /
to traverse back in the file structure.
"/res/shader/some_shader.glsl"
UPDATE
I have no idea what you're doing wrong, but I have no problems. You can see the code. I exported it to jar. It works both in Eclipse and in the jar
package stackoverflow;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class TestImagWithEclipse {
private static final String PATH = "/res/shader/stack_reverse.png";
public static void main(String[] args) {
URL url = TestImagWithEclipse.class.getResource(PATH);
ImageIcon icon = new ImageIcon(url);
JOptionPane.showMessageDialog(null, new JLabel(icon), "It works", JOptionPane.PLAIN_MESSAGE);
}
}