简体   繁体   English

无法从类路径加载属性文件

[英]Not able to loading properties file from classpath

I am running jar file using java -classpath "file_name.properties" -jar file_name.jar. 我正在使用java -classpath“ file_name.properties” -jar file_name.jar运行jar文件 This jar contains spring bean classes and xml's. 这个罐子包含Spring bean类和xml。 I configured file_name.properties file in spring xml using PropertyPlaceholderConfigurer bean like below. 我使用如下所示的PropertyPlaceholderConfigurer bean在spring xml中配置了file_name.properties文件。

<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. 但我得到FileNotFoundException如下所示。

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 谢谢,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 使用java -classpath "full-path-of-folder-that-contains-resources" -jar jar-to-run.jar
example: java -classpath C:\\java\\example -jar example.jar 示例: 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 并使用java -Dconfig-location=absolute_path_to_config_folder -jar your.jar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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