[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 706
  • Last Modified:

Read file within same JAR as applet

Not sure if this is possible, but here goes.
I currently have a JAR'd applet that reads a data file using the URL class. This opens a separate http connection after the JAR has unpacked & applet started.
I have added the data file to the JAR but the applet still looks for the file separately.
Seeing as there's no problem loading images from within the JAR, is there any way to read any type of file this way?
I know that I could store the file as a .GIF image and use PixelGrabber but this is a "last resort".
0
BOINGED
Asked:
BOINGED
  • 3
  • 2
1 Solution
 
Tomas Helgi JohannssonCommented:
  Hi!

Look at this API : http://java.sun.com/j2se/1.3/docs/api/java/util/jar/JarFile.html
There is a method (getInputStream ) which gives you access to the data in the specified file in the JARFile.
All you need to do is create an instance of the JARFile class.

Regards,
  Tomas Helgi
0
 
objectsCommented:
You can read any type of file from your jar.
Use the Class method getResourceAsStream() to open the file.
0
 
BOINGEDAuthor Commented:
Does anyone have some example code for either of the two solutions?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
objectsCommented:
InputStream in = getClass().getResourceAsStream(resourcename);
// then simply read your file from input stream
0
 
Tomas Helgi JohannssonCommented:
 Hi!

Look at this web page :
http://www.onjava.com/pub/a/onjava/2001/04/12/signing_jar.html?page=3
or this
http://www.superliminal.com/sources/JarLoader.java.html

Hope these examples help you.

Regards,
 Tomas Helgi
0
 
objectsCommented:
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now