Search code examples
javafilejarembedded-resource

File not found in jar


I'm working in java swing application. I'm using icon images which is placed inside uploads folder. When i run my project it's working fine.

I export my project as jar. When i run my jar file it show file not found error.

This is my code,

BufferedImage img = ImageIO.read(new File(Config.IMAGE_RESOURCE_FOLDER + "/" + fileName));

folder structure


Solution

  • I assume your File is in the jar. So put a class named Locator in the same Directory. and use following code:

    URL url = locator.getURL(name + ".gif"); Image img = Toolkit.getDefaultToolkit().getImage(url);

    The class Locator is:

    package schachfiguren; import java.net.*; public class Locator { public URL getURL(String pfad) { URL url = this.getClass().getResource(pfad); return url; } }

    pfad is the name of the File. For Example sbs.gif.