?
Solved

Can i keep the .properties file outside the src folder?

Posted on 2011-02-27
6
Medium Priority
?
547 Views
Last Modified: 2012-08-13
I am using properties file to set the configuartion for a small java application. At present the properties files lie in a package (called 'config') within the src folder. So the structure is

Project folder
                   |
                   - src
                         |
                         --com.abc.util (this is where the java class files are)
                         |
                         |
                         - config (this is where the properties files are, this is within the src folder)

I want to distribute this application as a jar. I want to the keep the properties files under the project folder directly not within the src folder. I s this a good practise?

Also is am using the following code to read and load the properties files.
InputStream propertyStream = PropertyLoader.class.getClassLoader()
				.getResourceAsStream(propertyFile);

		Properties properties = new Properties();
		try {
			properties.load(propertyStream);
		} catch (IOException e) {
			throw new RuntimeException(e.getMessage());
		}

Open in new window


How do i change the code to read files contained outside the src folder?

0
Comment
Question by:PearlJamFanatic
[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
  • 2
  • 2
  • 2
6 Comments
 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 2000 total points
ID: 34994325
It is a normal practice. For example, log4j and many other does the same

see this link
http://www.rgagnon.com/javadetails/java-0434.html
(Load a Properties File)
0
 
LVL 92

Expert Comment

by:objects
ID: 34994477
you can (and should) move it out of the source folder. With you're current code you need to include somewhere in your classpath
0
 

Author Comment

by:PearlJamFanatic
ID: 34994572
String path = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();	
		String[] arrPath=path.split("/");
		String projectFolderPath="";
		for (int i=0;i<=arrPath.length-2;i++)
			projectFolderPath=projectFolderPath+arrPath[i]+"/";			
		
		
		InputStream propertyStream = PropertyLoader.class.getClassLoader()
				.getResourceAsStream(propertyFile);

		Properties properties = new Properties();
		try {
			java.io.FileInputStream fis = new java.io.FileInputStream
			   (new java.io.File( projectFolderPath + propertyFile));
			properties.load(fis);
		} catch (IOException e) {
			throw new RuntimeException(e.getMessage());
		}
		return properties;
	}

Open in new window


It seems to be working. I have placed the properties files in folder config under the project folder. Is this what you mean gurvinder?
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 34994586
<< Is this what you mean gurvinder?>>
Not necessarily. You can keep it anywhere in your file system from where your program have the permissions to access that location.
0
 

Author Closing Comment

by:PearlJamFanatic
ID: 34994606
thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 34994608
> It seems to be working.

but will it work when you deploy the application?
The advantage of the code you were originally using is that it would search the classpath for the file.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

752 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