简体   繁体   中英

Not able to loading properties file from classpath

I am running jar file using java -classpath "file_name.properties" -jar file_name.jar. This jar contains spring bean classes and xml's. I configured file_name.properties file in spring xml using PropertyPlaceholderConfigurer bean like below.

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    <property name="locations">  
        <list>  
            <value>classpath:file_name.properties</value>
       </list>  
    </property>  
</bean>

But I'm getting FileNotFoundException like below.

java.io.FileNotFoundException: class path resource [einvoice-spring.properties] cannot be opened because it does not exist
        at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
        at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:153)

So please help me fix this issue.

Thanks, Narsi

Have you tried without the list tags ?

<bean class="org.springframework.beans.factory.config.PropertyPlaceHolderConfigurer">
  <property name="location" value="classpath:path/to/service.properties" />
</bean>

use java -classpath "full-path-of-folder-that-contains-resources" -jar jar-to-run.jar
example: java -classpath C:\\java\\example -jar example.jar

EDIT: alternative solution

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  <property name="locations">  
    <list>  
        <value>file:${config-location}/file_name.properties</value>
   </list>  
  </property>  
</bean>

and use java -Dconfig-location=absolute_path_to_config_folder -jar your.jar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM