Solved

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

Posted on 2011-02-27
6
475 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
<< 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
Comment Utility
thanks
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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:

763 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

6 Experts available now in Live!

Get 1:1 Help Now