Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-02-27
6
Medium Priority
?
585 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

604 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