• Post Reply Bookmark Topic Watch Topic
  • New Topic

reading config file from JAR file  RSS feed

 
Harris Tanu
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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
 
Joe Ess
Bartender
Posts: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!