This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Applets and the fly likes Reading from a JAR file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Reading from a JAR file" Watch "Reading from a JAR file" New topic
Author

Reading from a JAR file

Lee Feder
Greenhorn

Joined: Aug 11, 2000
Posts: 20
Is it possible to load data from a file held in one of the jar files in the ARCHIVE parameter?
Basically, I've got a couple of property files that I'd love the applet to read when it gets initialised.
Can anyone help?
Lee Feder
Greenhorn

Joined: Aug 11, 2000
Posts: 20
Oh I forgot, the applet must be able to run offline too.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Lee,
Here is how you would do it :

Where you would replace ClassName with the real name of your class, and image.gif with the real name of the image you are loading ( along with it's path in the JAR file if applicable ). This will work both on and offline.
HTH,
-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Lee Feder
Greenhorn

Joined: Aug 11, 2000
Posts: 20
Thanks Nate, it works a treat.
Khurram Fakhar
Ranch Hand

Joined: May 29, 2000
Posts: 65
Hi , ppl
It is working fine in IE but it is giving error with NS..
Kindly help me out
Regards
Khurram Fakhar
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Okay... Ummm... could you say what error it is giving in Netscape...
Khurram Fakhar
Ranch Hand

Joined: May 29, 2000
Posts: 65
It works fine with
URL url1 = ClassName.class.getResource("loginpage_image.jpg");
but on the second line it gives the following error:
line :
panelImage=Toolkit.getDefaultToolkit().getImage(url1);
error :
java.lang.NullPointerException
at sun.awt.SunToolkit.getImageFromHash(Compiled Code)
at sun.awt.SunToolkit.getImage(Compiled Code)
at ClientMain.images(Compiled Code)
at ClientMain.init(Compiled Code)
* at netscape.applet.DerivedAppletFrame$InitAppletEvent.dispatch(Compiled Code)
at java.awt.EventDispatchThread$EventPump.dispatchEvents(Compiled Code)
at java.awt.EventDispatchThread.run(Compiled Code)
at netscape.applet.DerivedAppletFrame$AppletEventDispatchThread.run(Compiled Code)

but working fine with IE .. ..
isn't it strange ?

regards
Khurram Fakhar
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Hmmm... I don't have Netscape here at work to test it out on, but have you tried to replace :


with :


( I assume you are doing this in an Applet... )

Hopefully this works... if not please post again...


-Nate
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Actually, ignore the previous post... I did a search on this and found that Netscape's getResource() method is broken...

Here's some code I found in a JavaWorld article ( http://www.javaworld.com/javaworld/jw-07-1998/jw-07-jar_p.html ) that should help you out...



You should call it in your code like this:


HTH,
-Nate
Khurram Fakhar
Ranch Hand

Joined: May 29, 2000
Posts: 65
Hay Thanx a million Man
It worked. I studied the whole article .. very well written article .
Thanx once again ..
Regards
Khurram Fakhar
 
Consider Paul's rocket mass heater.
 
subject: Reading from a JAR file