The Loader.getResource () class does not work from a .jar file

I have the following code:

private final ImageIcon placeHolder = new ImageIcon(this.getClass().getClassLoader().getResource("Cards\\trans.png"));

But this does not work when my application is exported to a file .jar.

+3
source share
2 answers

Your "\ t" compiled as a tab - you need to avoid this:

private final ImageIcon placeHolder = new ImageIcon(
    this.getClass().getClassLoader().getResource("Cards\\trans.png"));

Note the double backslash. This, of course, is not the only thing that is wrong, but this is the beginning ...

In fact, I would indicate it instead of a slash. In any case, this works on both Windows and Unix systems, and also works with jar files. The only reason I highlighted the double backslash was to raise the line drop point as a whole. Try the following:

private final ImageIcon placeHolder = new ImageIcon(
    this.getClass().getClassLoader().getResource("Cards/trans.png"));

, , . Windows , jar. "" "" "TRANS.png" "trans.png", .

+10

jar

ClassLoader.getSystemResource("images/Springz3.png"));

+2

Source: https://habr.com/ru/post/1721736/


All Articles