[LUNI] newbie java in html question

David Terrell dbt at meat.net
Tue Apr 13 13:39:42 CDT 2004


On Tue, Apr 13, 2004 at 12:16:25PM -0500, Carey Tyler Schug wrote:
> I cribbed an html page from the web, but it contained java code loading a file from the web.  Well, I don't want to be dependent upon an internet connection, so I did manage to figure out how to reference a local file on my computer instead.  However, I can't figure out how to make it relative to the current directory, so it doesn't survive being network loaded form another computer, or being sent via e-mail.
> 
> Any suggestions on this?  If nothing else, can I convert it to a hex character string and just include it as a value in-line?
> 
> Image2=new Image();
> Image2.src=grphcs[2]="file:///c:/shared/html/bday_files/candle.gif"

This URL right here makes it offtopic for a linux users list, but ...

The right way to do this is to include candle.gif in the jar file that
contains your app.  If it's at the root, you can basically do:

import javax.imageio.ImageIO;
import java.awt.Image;

Class Foo
{
    Image image = ImageIO.read(Foo.class.getResourceAsStream('/candle.gif'));
}

if Foo is in package com.foo.bar, you can also put candle.gif in
/com/foo/bar/images/candle.gif in the jar file, and call
getResourceAsStream('images/candle.gif') ... relative pathnames are
expanded relative to the base of the package path.  Check out the 
javax.imageio package and Class.getResourceAsStream javadoc entry
for more background.

(note:  completely untested code, check for typos, etc)



More information about the luni mailing list