Solved

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

Posted on 2014-12-24
3
230 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
3 Comments
 
LVL 26

Assisted Solution

by:dpearson
dpearson earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
A short article about a problem I had getting the GPS LocationListener working.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now