title: Java - How to access an image packed in an applet jar
body: <p>I have created an applet jar. That jar contains an images in the following folder</p>
  
  <pre><code>com\common\images\red.bmp
  </code></pre>
  
  <p>Now, I want to display this image on the Swing Applet.</p>
  
  <pre><code>private static final ImageIcon redIndicator = new ImageIcon("com\\common\\images\\red.bmp");
  </code></pre>
  
  <p>After that, I have attached the <strong>redIndicator</strong> to a <code>JPanel</code> but I am not able to see this image.</p>
  
  <p>Any suggestions?</p>
  
  <p>==================================EDITED=========================================</p>
  
  <pre><code>private static final ImageIcon marker = loadImage("com/common/images/scale.jpg");
  
  @SuppressWarnings("unused")
  private static ImageIcon loadImage(String imagePath) {
  
      BufferedInputStream imgStream = new BufferedInputStream(TpcHandler.class.getResourceAsStream(imagePath));
      int count = 0;
  
      if (imgStream != null) {
  
          byte buf[] = new byte[2400];
  
          try {
              count = imgStream.read(buf);
          } catch (java.io.IOException ioe) {
              return null;
          } finally {
              if (imgStream != null)
                  try {
                      imgStream.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
          }
  
          if (count &lt;= 0) {
              LOGGER.warning("Empty image file: " + imagePath);
              return null;
          }
  
          return new ImageIcon(Toolkit.getDefaultToolkit().createImage(buf));
      } else {
          LOGGER.warning("Couldn't find image file: " + imagePath);
          return null;
      }
  }
  </code></pre>
  
  <p>I am getting the following exception</p>
  
  <pre><code>java.io.IOException: Stream closed
  </code></pre>
  
  <p>at line <code>count = imgStream.read(buf);</code></p>