The path to retrieving resources using ClassLoader

Basically, I want to include my main icon JFramein a JAR file, so no need to download it from an external location.

For this, I was looking for a Java resource system. What I did with Eclipse:

  • I created a new folder called res:

  • I copied the files inside it using Windows Explorer:

  • I made this folder the original folder:

  • I wrote this code:

    URL url = ClassLoader.getSystemResource("/res/icona20.ico");
    

But urlthere is null. What have I done wrong?

+4
source share
2 answers

As already mentioned, you added resas the source folder, so this is the root, not the name, for example src.

URL url = ClassLoader.getSystemResource("icona20.ico");

( ) , /....

:

URL url = Xyz.class.getResource("/icona20.ico");

.png .ico, Java SE.

( .) maven :

/src/main/java/
/src/main/resources/
/src/test/java/
/src/test/resources/

res MS Visual Studio;).

+3

, , . , URL- :

URL url = ClassLoader.getSystemResource("icona20.ico");
+3

All Articles