• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Loading Resources within *.jar

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
John Luckcuck
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic