• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 2662
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2662
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2662
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Do I use an InputStream instead?


Yes.

Regards, Jan
 
Danger, 10,000 volts, very electic .... tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic