Java Swing: Displaying images from within a Jar

后端 未结 5 1202
野趣味
野趣味 2020-11-22 15:13

When running a Java app from eclipse my ImageIcon shows up just fine.

But after creating a jar the path to the image obviously gets screwed up.

Is there a wa

5条回答
  •  无人及你
    2020-11-22 15:56

    You can try something like:

    InputStream stream = this.getClass().getClassLoader().getResourceAsStream("/images/image.jpg");
    

    In your JAR file, you might have a directory structure of:

    MyJAR.jar
    - com (class files in here)
    - images
    ----image.jpg

提交回复
热议问题