Weird problem with images on JApplet

Hi

I was using jdk 1.5 and the images (JPG and GIF) were displayed perfectly in ImageIcons, I uploaded the web page and it worked very well.

that was until 4 or 5 days when I visited the web site and the miniapplication was not able to load I look in the java console and thereafter I noticed the images didin't load, then I checked the sun website and they used another method different from getImage(getDocumentBase(),"something.jpg").

it goes like this:

String path = "something.jpg";

int MAX_IMAGE_SIZE =45653;

//Change this to the size of

//your biggest image, in bytes.

int count = 0;

BufferedInputStream imgStream = new BufferedInputStream( this.getClass().getResourceAsStream(path));

if (imgStream != null)

{

byte buf[] = new byte[MAX_IMAGE_SIZE];

try

{

count = imgStream.read(buf);

imgStream.close();

}

catch (java.io.IOException ioe)

{

System.err.println("Couldn't read stream from file: " + path);

return null;

}

catch(Exception e)

{

e.printStackTrace();

}

if (count <= 0)

{

System.err.println("Empty file: " + path);

return null;

}

return new ImageIcon(Toolkit.getDefaultToolkit().createImage(buf));

//return new ImageIcon(Toolkit.getDefaultToolkit().createImage(path));

}

else

{

System.err.println("Couldn't find file: " + path);

return null;

}

I wrote and recompiled the JApplet and it worked OK, then I upload it to the website and opened it from there and... EXCEPTION!

sun.awt.image.ImageFormatException: JPEG datastream contains no image

at sun.awt.image.JPEGImageDecoder.readImage(Native Method)

at sun.awt.image.JPEGImageDecoder.produceImage(Unknown Source)

at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)

at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)

at sun.awt.image.ImageFetcher.run(Unknown Source)

I updated the jdk and I repeated the process but is still the same.

SOMEBODY HELP ME PLEASE!

[2162 byte] By [Cesar_Castroa] at [2007-11-27 6:38:50]
# 1
Your image loading method for applet is wrong.And if it is for an ordinary application, still your stream reading code is wrong.
hiwaa at 2007-7-12 18:07:44 > top of Java-index,Java Essentials,Java Programming...
# 2
Yes, i know something is wrong, but:1) why it works perfectly when I run it from any computer hard drive but doesn't work when I go to the web site and run it from there?2) if you know it's wrong, what's your theory about how correct it?
Cesar_Castroa at 2007-7-12 18:07:44 > top of Java-index,Java Essentials,Java Programming...
# 3
For Applet, use Applet.getImage() method with the URL derived from your codebase or documentbase. For general stream read, you should always do it in a loop in which you constantly check the return value from the read() method.
hiwaa at 2007-7-12 18:07:44 > top of Java-index,Java Essentials,Java Programming...
# 4

> Yes, i know something is wrong, but:

> 1) why it works perfectly when I run it from any

> computer hard drive but doesn't work when I go to the

> web site and run it from there?

An applet is run on the client side, so it's looking in the current directory from whatever temp directory the browser decided to execute the applet from.

> 2) if you know it's wrong, what's your theory about

> how correct it?

You should use Applet.getImage(), as hiwa said, or you can pack the image into your jar, and use Class.getClassloader().getResource() to load it up.

kevjavaa at 2007-7-12 18:07:44 > top of Java-index,Java Essentials,Java Programming...
# 5

First of all thanks for your help, I really apreciate it.

ok, I tried this:

img=getImage(getDocumentBase(),"something.jpg");

//at this line img is still null !!! NULLPOINTEREXCEPTION !!!

imgicon=new ImageIcon(img.getScaledInstance(225,118,img.SCALE_SMOOTH));

which is what exactly how it was before, but again, runs on the hard drive but not on the web site, and worst, about 4 months ago was running on the web site and I have already make sure that the files were not corrupted.

since the great amount of resources of the web page, packing everithing on a jar is an option I cannot afford since the jar becomes too heavy (and for the same reason I haven't been able to try that theory)

What should I do?

Cesar_Castroa at 2007-7-12 18:07:44 > top of Java-index,Java Essentials,Java Programming...
# 6

> First of all thanks for your help, I really apreciate

> it.

>

> ok, I tried this:

>

> img=getImage(getDocumentBase(),"something.jpg");

> //at this line img is still null !!!

> NULLPOINTEREXCEPTION !!!

> imgicon=new

> ImageIcon(img.getScaledInstance(225,118,img.SCALE_SMOO

> TH));

>

> which is what exactly how it was before, but again,

> runs on the hard drive but not on the web site, and

> worst, about 4 months ago was running on the web site

> and I have already make sure that the files were not

> corrupted.

>

> since the great amount of resources of the web page,

> packing everithing on a jar is an option I cannot

> afford since the jar becomes too heavy (and for the

> same reason I haven't been able to try that theory)

>

> What should I do?

Try that theory anyway.

abillconsla at 2007-7-12 18:07:44 > top of Java-index,Java Essentials,Java Programming...
# 7

Hi

Thank you very much.

I packaged everything on a jar and it worked perfectly and i didin't change the code a bit, fortunately, the jar doesn't take much time to load.

just for curiosity i packaged every code (the getresourceasstream, the getdocumentbase, the getresource and the getdocument base) and every one worked just the same so, the code wasn't wrong after all.

All i have left to do is supose that if a JApplet isn't on a jar it doesn't load images (something odd if you ask me) on the web.

Thanks to all of you again.

Cesar_Castroa at 2007-7-12 18:07:44 > top of Java-index,Java Essentials,Java Programming...
# 8
No that is incorrect. But it's easier in many cases to jar them up.
abillconsla at 2007-7-12 18:07:44 > top of Java-index,Java Essentials,Java Programming...