🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.api
Article #3001

can't load image from jar file..

#3001
From: raghuv
Date: Sat, 20 Jun 1998 00:00
77 lines
2701 bytes
Hello,

I am still having problem reading image from the jar file.
I am trying this on Netscape 4.0.5,
as suggested in Netscape website somewhere, I am using
getResourceAsStream() to load my image from jar file.
But I get null when I call this function.
Could some one tell me what is wrong here (below) ?
Does it work on 4.0.5 ?

Thanks a lot,

-raghu

Here is my code: (only 45 lines)
----------------TestImage.java--------------
package test;


import java.awt.*;
import java.io.*;
import com.sun.java.swing.*;

public class TestImage extends JApplet  {

  public void init()  {
    super.init();

    Icon myIcon = null;
    try {

      String imageFile = "some.gif";
      InputStream imageStream =
this.getClass().getResourceAsStream(imageFile);
      if (imageStream == null) { // I AM GETTING NULL HERE
        // TODO:Dialog here, couldn't locate the image
        System.err.println("Unable locate image: " + imageFile);
      }
      else {
        System.out.println("Number of bytes available = " +
imageStream.available());
        DataInputStream dataStream = new DataInputStream( imageStream);
        byte[] imageBuffer = new byte[dataStream.available()];
        dataStream.readFully(imageBuffer);
        // ImageIcon takes care of waiting until the whole image is
loaded
        // no need for a media tracker ??
        myIcon = new ImageIcon( imageBuffer);
        imageStream.close();
      }
    }
    catch (java.io.IOException e) {
       // TODO Dialog here
       System.err.println("Unable to read image.");
       e.printStackTrace();
    }

   if( myIcon != null)
    getContentPane().add( new JButton("Loaded Icon successfully",
myIcon), BorderLayout.CENTER);
   else
    getContentPane().add( new JButton("Couldn't load Icon "),
BorderLayout.CENTER);
  }
}
-------------------- TestImage.html
<HTML><HEAD><TITLE> DefaultApp </TITLE>
</HEAD>
<BODY> <applet archive="test.jar" code="test.TestImage" width=400
height=300
</applet>
</BODY>
</HTML>

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading
Technical Details
Message-ID: <6mfonv$64a$1@nnrp1.dejanews.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1089d0,2bc4eac1631a89a5,start
X-Google-Attributes: gid1089d0,public
From: raghuv@my-dejanews.com
Subject: can't load image from jar file..
Date: 1998/06/20
Message-ID: <6mfonv$64a$1@nnrp1.dejanews.com>
X-Deja-AN: 364417111
X-Http-User-Agent: Mozilla/4.05 [en] (WinNT; I)
Organization: Deja News - The Leader in Internet Discussion
X-Article-Creation-Date: Sat Jun 20 07:35:27 1998 GMT
Newsgroups: borland.public.jbuilder.java.api