Add image to JAR Java

First step: Put your image in a defined location in your JAR-file. If you put it into the src-folder, maybe your IDE or your build-tool will package it to the JAR automatically. Otherwise check the documentation of your IDE/build-tool, in which location you have to put it.

Second step: Access the file from your program. I assume you put it in the package (the path in your JAR) package1/package2 and the file is called dump.jpg. In that case you call:

setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/package1/package2/dump.jpg")));

getClass().getResource(…) returns an URL for a resource on your classpath. You start the path in the classpath with a “https://stackoverflow.com/” and use the complete path/packages to your resource, separated by “https://stackoverflow.com/”.

Leave a Comment