• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

reading config file from JAR file

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to read file within a jar file?

This is the scenario:
I created a class which requires a config file. I thought of jar-ing them together. And when I execute the file which reads the config file in, it doesn't seem to find the config file.

What is the way for java class bundled in a JAR to read the file bundled together? Or whether it is possible?

tx
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the JAR is in the classpath, you can use Class.getResource() or Class.getResourceAsStream() to read a file. If not, you can unzip the file into a temp directory using the java.util.jar library.
 
Harris Tanu
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, probably I didn't describe correctly.

Example I have a java class ReadFile.java which requires abc.properties
Is it possible to ship those 2 files as a jar because ReadFile.java needs to read abc.properties?

My experience when I try to read the file abc.properties, it fails to find the file because it looks for the current working directory (while the abc.properties is found inside the jar file).

any idea?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Joe said, in that case you can use the getResource() family of methods. Check out the Javadocs for more details here and here.
 
reply
    Bookmark Topic Watch Topic
  • New Topic