Results 1 to 7 of 7

Thread: Unable to locate prop file

  1. #1
    Join Date
    Jan 2011
    Posts
    5

    Default Unable to locate prop file

    I have placed my properties file outside my jar file, so that it can edited later in future. But with the below code its not able to read my properties file.

    Code:
    package com.abc.gip.mailer.utils;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.stereotype.Component;
    
    @Component("mailerProperties")
    public class MailerProperties {
    
    	private static Properties properties;
    	
    	public MailerProperties() {
    		properties = new Properties();
    		InputStream inStream;
    		try {
    			inStream = (new ClassPathResource("mailer.properties")).getInputStream();			
    			properties.load(inStream);
    		} catch (IOException e) {			
    			e.printStackTrace();
    		}	
    	}
    	
    	public String getValue(String key) {
    		return properties.getProperty(key);
    	}
    
    	
    }
    Last edited by nn12; Feb 3rd, 2011 at 09:35 AM.

  2. #2
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Default

    Hello

    1)Can you post your error stack trace?
    2) indicate where is located your jar file
    3) indicate where is located your .properties file

    for 2 and 3 I guess both are located in the classpath of your application, right?
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  3. #3
    Join Date
    Jan 2011
    Posts
    5

    Default

    I have placed both the jar file & the mailer.properties inside D:\emailer folder and I run the jar file using D:\emailer>java -jar test.jar

    java.io.FileNotFoundException: class path resource [mailer.properties] cannot be
    opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getI nputStream(ClassPat
    hResource.java:158)
    at com.abc.gip.mailer.utils.MailerProperties.<init>(M ailerPropertie
    s.java:20)

  4. #4
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    Since you are using Spring why not loading properties files "the Spring way"? Use the utilroperties tag in Spring xml config file to register a bean of type java.util.Properties with your configuration loaded:

    Code:
    <util:properties id="mailerProperties" location="classpath:mailer.properties"/>
    Then you can access properties using Spring EL, both in xml and using the @Configuration and @Value javaconfig annotations on your beans (or you can dependency inject the mailerProperties bean itself into components that need to use it).

    Or, if you prefer old xml configuration, use PropertyPlaceHolderConfigurer:

    Code:
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    	<property name="location">
    	  <value>classpath:mailer.properties</value>
    	</property>
      </bean>
    to obtain the same results.

  5. #5
    Join Date
    Jan 2011
    Posts
    5

    Default

    I exactly have this same piece of code in my application context xml
    Code:
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    	<property name="location">
    	  <value>classpath:mailer.properties</value>
    	</property>
      </bean>
    And when I run the execute the jar file with the properties file placed outside I get

    Exception in thread "main" org.springframework.beans.factory.BeanInitializati onE
    xception: Could not load properties; nested exception is java.io.FileNotFoundExc
    eption: class path resource [mailer.properties] cannot be opened because it does
    not exist

  6. #6
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    Is the directory where you put the external file part of the application classpath? Evidently it's not...so either add the directory to classpath or (better option) use file:/path/to/your/file/mailer.properties instead of using classpath:

  7. #7
    Join Date
    Jan 2011
    Posts
    5

    Default

    Thanks Enrico for all your help.
    Code:
    <property name="location" value = "file:D:\emailer\mailer.properties" />
    worked for me.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •