in eclipse: you can place image files along with java source code files, if you are putting it in other directory then make sure you create that directory as src directory. and create packages same as source code packages, and copy image files to those created packages.

for example:

  src (source directory)
  resource (source directory)
                                ^................ logo.png

then try to export this project as jar file.


you need to put your images inside of a source folder and ensure that the settings for that folder don't exclude what you are trying to export.

try setting up a clean java project to ensure that it all works as expected with your version of eclipse to see what may be missing. create a new project following these instructions using the project dialog:

  1. file > new > java project
  2. in the first dialogue step project layout > configure default... change the source folder name to src/main/java which is the convention followed by tools like gradle and maven so it is the project layout for most opensource projects so its a good convention to stick with.
  3. second dialogue step create new source folder give it the name src/main/resources which is again the standard naming.

create the new project and drag-drop an image into src/main/resources e.g. test.jpg then add a java class:

public class test {
    public static void main(string[] args) throws exception {

run it in eclipse to check it gives you the image file size. then do file > export > jar file and export it to /tmp/test.jar and run it with java -cp /tmp/test.jar to see that it gives you the output.

that all works for me. go back through that project settings and compare it with the one you are trying to setup. note that there are options to include/exclude files which get exported by filename so it may be the case what you have an export filter which is stopping your files being exported. my recommendation is that you put all code under src/main/java and all resources such as images and config files under src/main/resources then have no export filters which is how build tools such as maven and gradle do things.


maven is my suggestion

if you're following the standard maven project directory structure then it is best to put all non-java resources under src/main/resources. for example, you could create a subdirectory images, so that the full path would be src/main/resources/images. this directory would contain all your application images.

a special care should be taken to properly access images when application is packaged. for example, the following function should do everything you need.

public static image getimage(final string pathandfilename) {
    final url url = thread.currentthread().getcontextclassloader().getresource(pathandfilename);
    return toolkit.getdefaulttoolkit().getimage(url);

this function can be used as getimage("images/some-image.png") in order to load some-image.png file in the image directory.

if imageicon is required then simply calling new imageicon(getimage("images/some-image.png")) would do the trick.

Related Query

More Query from same tag