Loading a properties file from Java package

JavaProperties File

Java Problem Overview


I need to read a properties files that's buried in my package structure in com.al.common.email.templates.

I've tried everything and I can't figure it out.

In the end, my code will be running in a servlet container, but I don't want to depend on the container for anything. I write JUnit test cases and it needs to work in both.

Java Solutions


Solution 1 - Java

When loading the Properties from a Class in the package com.al.common.email.templates you can use

Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream("foo.properties");
prop.load(in);
in.close();

(Add all the necessary exception handling).

If your class is not in that package, you need to aquire the InputStream slightly differently:

InputStream in = 
 getClass().getResourceAsStream("/com/al/common/email/templates/foo.properties");

Relative paths (those without a leading '/') in getResource()/getResourceAsStream() mean that the resource will be searched relative to the directory which represents the package the class is in.

Using java.lang.String.class.getResource("foo.txt") would search for the (inexistent) file /java/lang/String/foo.txt on the classpath.

Using an absolute path (one that starts with '/') means that the current package is ignored.

Solution 2 - Java

To add to Joachim Sauer's answer, if you ever need to do this in a static context, you can do something like the following:

static {
  Properties prop = new Properties();
  InputStream in = CurrentClassName.class.getResourceAsStream("foo.properties");
  prop.load(in);
  in.close()
}

(Exception handling elided, as before.)

Solution 3 - Java

The following two cases relate to loading a properties file from an example class named TestLoadProperties.

Case 1: Loading the properties file using ClassLoader

InputStream inputStream = TestLoadProperties.class.getClassLoader()
                          .getResourceAsStream("A.config");
properties.load(inputStream);

In this case the properties file must be in the root/src directory for successful loading.

Case 2: Loading the properties file without using ClassLoader

InputStream inputStream = getClass().getResourceAsStream("A.config");
properties.load(inputStream);

	

In this case the properties file must be in the same directory as the TestLoadProperties.class file for successful loading.

Note: TestLoadProperties.java and TestLoadProperties.class are two different files. The former, .java file, is usually found in a project's src/ directory, while the latter, .class file, is usually found in its bin/ directory.

Solution 4 - Java

public class Test{	
  static {
	loadProperties();
}
   static Properties prop;
   private static void loadProperties() {
	prop = new Properties();
	InputStream in = Test.class
			.getResourceAsStream("test.properties");
	try {
		prop.load(in);
		in.close();
	} catch (IOException e) {
		e.printStackTrace();
	}

}

Solution 5 - Java

public class ReadPropertyDemo {
	public static void main(String[] args) {
		Properties properties = new Properties();

		try {
			properties.load(new FileInputStream(
					"com/technicalkeeda/demo/application.properties"));
			System.out.println("Domain :- " + properties.getProperty("domain"));
			System.out.println("Website Age :- "
					+ properties.getProperty("website_age"));
			System.out.println("Founder :- " + properties.getProperty("founder"));
			
			// Display all the values in the form of key value
			for (String key : properties.stringPropertyNames()) {
				String value = properties.getProperty(key);
				System.out.println("Key:- " + key + "Value:- " + value);
			}

		} catch (IOException e) {
			System.out.println("Exception Occurred" + e.getMessage());
		}

	}
}

Solution 6 - Java

Assuming your using the Properties class, via its load method, and I guess you are using the ClassLoader getResourceAsStream to get the input stream.

How are you passing in the name, it seems it should be in this form: /com/al/common/email/templates/foo.properties

Solution 7 - Java

I managed to solve this issue with this call

Properties props = PropertiesUtil.loadProperties("whatever.properties");

Extra, you have to put your whatever.properties file in /src/main/resources

Solution 8 - Java

Nobody mentions the similar but even simpler solution than above with no need to deal with the package of the class. Assuming myfile.properties is in the classpath.

        Properties properties = new Properties();
        InputStream in = ClassLoader.getSystemResourceAsStream("myfile.properties");
        properties.load(in);
        in.close();

Enjoy

Solution 9 - Java

use the below code please :

Properties p = new Properties();
StringBuffer path = new StringBuffer("com/al/common/email/templates/");
path.append("foo.properties");
InputStream fs = getClass().getClassLoader()
.getResourceAsStream(path.toString());

if(fs == null){
     System.err.println("Unable to load the properties file");
  }
else{
   try{
       p.load(fs);
     } 
catch (IOException e) {
        e.printStackTrace();
      }
   }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSacramentoJoeView Question on Stackoverflow
Solution 1 - JavaJoachim SauerView Answer on Stackoverflow
Solution 2 - Javacobra libreView Answer on Stackoverflow
Solution 3 - JavaKulDeepView Answer on Stackoverflow
Solution 4 - Javauser897493View Answer on Stackoverflow
Solution 5 - JavaVickyView Answer on Stackoverflow
Solution 6 - JavaChris KimptonView Answer on Stackoverflow
Solution 7 - JavaNaramsimView Answer on Stackoverflow
Solution 8 - Javaisaac.hazanView Answer on Stackoverflow
Solution 9 - JavaPrithvish MukherjeeView Answer on Stackoverflow