Solved

Is it possible to read a properties file from a jar?

Posted on 2014-12-24
3
240 Views
Last Modified: 2014-12-28
Hello,

I was just wondering if it was possible to read a properties file that was bundled with another jar file?

Appreciate any help or alternatives! (aside from including the properties file here... lol)
0
Comment
Question by:Errang Genevre
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 27

Assisted Solution

by:dpearson
dpearson earned 250 total points
ID: 40517320
Yes that should be possible.

It's been a while since I wrote any code like this (it always seems rather clumsy how you get resources and class loaders) but I think it's something like this sequence.  (There may be short cuts available to reduce the number of calls here - as I say it's been a while).

The basic idea is you get a class loader for the Jar file and then open a stream within the jar and read the properties file.

Along these lines (this code is just approximate - you'll likely need to tweak it):

	private static ClassLoader getClassLoader(File confFolder) throws Exception {
		ClassLoader loader = MyClass.class.getClassLoader();

		if (confFolder != null) {
			URL confURL = confFolder.toURI().toURL() ;
			URLClassLoader cls = new URLClassLoader(new URL[]{ confURL }, loader) ;
			loader = cls ;
		}
		
		return loader ;
	}

        // ConfFolder in your case should point to the jar file
        // propertiesFile would be the path within the jar
        public static Properties loadPropertiesFile(File confFolder, File propertiesFile) throws Exception {
		ClassLoader cls = getClassLoader(confFolder) ;
		InputStream input = cls.getResourceAsStream(propertiesFile); 
		Properties properties = new Properties() ;
		properties.load(input) ;
                return properties ;
      }

Open in new window


Hope that helps,

Doug
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 40517471
InputStream in = getClass().getResourceAsStream("/p.properties");

Open in new window

is really all you need. The path is package-relative
0
 

Author Closing Comment

by:Errang Genevre
ID: 40520679
Thanks!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
spring maven example issues 3 44
AvlTree-Node Data type 4 28
Is online banking safe? 11 92
$_GET call between URL 3 45
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question