Solved

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

Posted on 2011-02-27
6
521 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:
gurvinder372 earned 500 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 40

Expert Comment

by:gurvinder372
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

688 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