• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is there a way I can store pictures/sounds into my Jar file?  RSS feed

 
Paul Carter
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to write a very basic game in Java and would like to store and fetch the images and sounds I play/show from the jar file itself instead of having to distribute the sounds/images in a separate folder.

What's the easiest/best way to acheive this?
 
Jan Cumps
Bartender
Posts: 2632
17
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It' possible.
You can place them in a jar, and load them later with ClassLoader.getResource().

The jar needs to be in your classpath.

Regards, Jan
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jan, you said the jar must be in your classpath..

like in the eviromental variables or is there some classpath that
you code?

Justin
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can load images with URLConnection and load sounds with the JarEntry class and acquire an input stream. I made a post about sound files and jars a while back. I think it was in the java in general intermediate forum. It has a small code fragment that loads files using JarEntry
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the JAR needs to be in the classpath, but if the Java program itself (the game Paul is talking about) is in the same JAR file, then it will already be in the classpath. So there's nothing special you have to do with the classpath.

The easiest way to access files like images etc. that are inside a JAR is by doing something like this:

The standard Java library also contains classes to work with JAR files that John is hinting at in the package java.util.jar, but the method above is the easiest way.
 
Jan Cumps
Bartender
Posts: 2632
17
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Justin Fox:
jan, you said the jar must be in your classpath..

like in the environmental variables or is there some classpath that
you code?

Justin


Either in the environment variable, or passed to java command line:

java -cp path/to/mylibrary.jar .....

Regards, Jan
 
Paul Carter
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FileInputStream in = getClass().getResourceAsStream("image.jpg");
BufferedImage image = ImageIO.read(in);
in.close();


Cheers guys, but on the first line I get an error:
Incompatible Types
found : java.io.InputStream
required: java.io.FileInputStream
FileInputStream in = getClass().getResourceAsStream("image.jpg");

Do I use an InputStream instead?
 
Jan Cumps
Bartender
Posts: 2632
17
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do I use an InputStream instead?

Yes.

Regards, Jan
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!