getResources() for ImageIcon - java

前端 未结 3 1658
故里飘歌
故里飘歌 2020-12-21 03:49

my very first question:

I\'ve been trying to figure this out for a couple of days, but I got to the point I lost my patience. The following are some code and my proj

相关标签:
3条回答
  • 2020-12-21 04:00

    src directory is not available in classpath

    InputStream imageInputStream = getClass().getClassLoader().getResourceAsStream("resources/icons/xyz.png");
    byte[] imageData = org.apache.commons.io.IOUtils.toByteArray(in)
    
    ImageIcon imageIcon = new ImageIcon(imageData, "description about image");
    
    0 讨论(0)
  • 2020-12-21 04:13

    If the png files have been packaged into the JAR in the same locations as they are in the original src directory then

    XXX("resources/icons/xyz.png");
    

    should produce the right result.

    0 讨论(0)
  • 2020-12-21 04:14

    Normally when exporting a JAR file from Eclipse, the contents of src/resources are exported minus the src path name itself. To get the image to load from your JAR you could do:

    InputStream stream = getClass().getResourceAsStream("/resources/icons/xyz.png");
    ImageIcon icon= new ImageIcon(ImageIO.read(stream));
    

    One way to know for sure is to check:

    jar tvf yourjar.jar
    
    0 讨论(0)
提交回复
热议问题