How to includes all images in jar file using eclipse

后端 未结 4 1599
生来不讨喜
生来不讨喜 2020-11-29 10:38

I made a java application and bundled all classes in a jar file. When I run the project from eclipse, my application is running successfully. But when I try to run my

相关标签:
4条回答
  • 2020-11-29 11:03

    The files in jar files are treated as "Resources". you need to access them as a classpath resource, regular File access methods does not work there.

    Try this:

    final public ImageIcon iReport = (new ImageIcon(getClass().getResource("images/Report.png")));
    
    0 讨论(0)
  • 2020-11-29 11:10

    I know this was asked long ago, but it might help others with the same problem, like me. I was already using getClass().getResource("..."), but the resource didn't get exported with .jar file. I solved the problem by refreshing the 'Resources' folder, and its every subfolder.

    0 讨论(0)
  • 2020-11-29 11:16

    You need to get it from the classpath instead of from the local disk file system.

    Assuming that images is actually a package and that this package is inside the same JAR as the current class, then do so:

    final public ImageIcon iReport = 
        new ImageIcon(getClass().getResource("/images/Report.png"));
    
    0 讨论(0)
  • 2020-11-29 11:16

    100% works

    final public ImageIcon iReport = new ImageIcon(getClass().getResource("/Report.png"));
    

    Don't forget about "/" at path for image.

    0 讨论(0)
提交回复
热议问题