This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Loading Resources within *.jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Loading Resources within *.jar" Watch "Loading Resources within *.jar" New topic
Author

Loading Resources within *.jar

John Luckcuck
Greenhorn

Joined: Apr 18, 2002
Posts: 7
I'm trying to load a properties file located /config/fbn.props in my jar. This works fine outside the jar. I'm using:

If I use the same principle for loading an image, this works fine :

I assume the problem is comming up with a URL that is compatible with the "internal references" of the jar. What's the best way (or working way even!) of obtaining the path to files within the jar ?
Thanks in advance


J2EE Web Developer
John Luckcuck
Greenhorn

Joined: Apr 18, 2002
Posts: 7
I've resolved this issue by using :
URL url = this.getClass().getResource("/config/fbn.props");
properties.load(url.openStream());
But maybe using a properties file was a little OTT in the first place :roll:
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
That's the way to do it -- the URL you get back will have the jar: pseudo-protocol and cannot be converted into a File. Using getResourceAsStream can simplify your code slightly.
- Peter
John Luckcuck
Greenhorn

Joined: Apr 18, 2002
Posts: 7
Cheers Peter,
I've decided to stick with my properties file and leave it outside the jar (which is sensible) to allow users to pre-configure their client / server without having to specify command line parameters. Of course, I wont be ignoring the command line parameters, the properties file will merely define my defaults. Any extra command line parameters will overwrite those used in the properties file.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Loading Resources within *.jar
 
Similar Threads
the pictures are not loaded into an executable jar file
Copy files within JAR to disk
ClassLoader and directories
Accessing resources in a JAR
ClassLoader and directories