Announcement Announcement Module
Collapse
No announcement yet.
Unable to locate prop file Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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, 10:35 AM.

  • #2
    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?

    Comment


    • #3
      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)

      Comment


      • #4
        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.

        Comment


        • #5
          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

          Comment


          • #6
            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:

            Comment


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

              Comment

              Working...
              X