Jump to content

Load image from inside jar

- - - - -

  • Please log in to reply
8 replies to this topic

#1
Pow

Pow

    Newbie

  • Members
  • PipPip
  • 11 posts
I'm currently using

Image img = Toolkit.getDefaultToolkit().getImage("./data/icon.png");

And it works great, but in this case I must place this image file next to jar for it to display...
and it isnt the best way to do it...

I have read everything I can find in google about reading image from inside a jar but nothing seems to work :(

Is there any reliable way to do it?

#2
isuru

isuru

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 233 posts
You can use,

Image img = getImage(getDocumentBase(), "book.gif");

Lost!

#3
Pow

Pow

    Newbie

  • Members
  • PipPip
  • 11 posts

isuru said:

You can use,
Image img = getImage(getDocumentBase(), "book.gif");
It doesnt work...
The method getDocumentBase() is undefined for the type Application	Application.java	/Attempt3/src/simple/game/engine	line 23	Java Problem


#4
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
I do something like this:
image = ImageIO.read(ClassLoader.getSystemResourceAsStream(imagePath+"imageName.gif"));


#5
Pow

Pow

    Newbie

  • Members
  • PipPip
  • 11 posts

eafkuor said:

I do something like this:
image = ImageIO.read(ClassLoader.getSystemResourceAsStream(imagePath+"imageName.gif"));
Well... for me it doesn't work too :(

Image icon = ImageIO.read(Application.getSystemResourceAsStream("./data/icon.png"));

error:

The method getSystemResourceAsStream(String) is undefined for the type Application	Application.java	/Attempt3/src/simple/game/engine	line 24	Java Problem



#6
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
Why Application? you must call ClassLoader.getSystemResourceAsStream, not Application.getSystemResourceAsStream!

#7
Pow

Pow

    Newbie

  • Members
  • PipPip
  • 11 posts
That doesnt work either way, that's why i tried to change it.
If it's

Image sprites = ImageIO.read(ClassLoader.getSystemResourceAsStream("data/sprites.png"));

error:

Unhandled exception type IOException	Application.java	/Attempt3/src/simple/game/engine	line 28	Java Problem



#8
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

Quote

I must place this image file next to jar
This actually makes it sound as if the images is outside the .jar file in the same directory. But it isn't, right?
Next, could you tell us if the class you're trying to open the image file from is in a package? (if so, provide the full name, please)
AND do you put the images in the root of the jar-file, or deeper( like in a "/Images" directory)?

#9
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts

Pow said:

That doesnt work either way, that's why i tried to change it.
If it's

Image sprites = ImageIO.read(ClassLoader.getSystemResourceAsStream("data/sprites.png"));

error:

Unhandled exception type IOException	Application.java	/Attempt3/src/simple/game/engine	line 28	Java Problem


Of course you're getting that.
Replace that line with this:

try {

    Image sprites = ImageIO.read(ClassLoader.getSystemResourceAsStream("data/sprites.png"));

} catch(IOException e) {

    System.out.println(e);

}





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users