Solved

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

Posted on 2011-02-27
6
496 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
  • 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

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…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

821 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