簡體   English   中英

無法從類路徑加載屬性文件

[英]Not able to loading properties file from classpath

我正在使用java -classpath“ file_name.properties” -jar file_name.jar運行jar文件 這個罐子包含Spring bean類和xml。 我使用如下所示的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>

但我得到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)

因此,請幫助我解決此問題。

謝謝,Narsi

您是否嘗試過沒有列表標簽?

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

使用java -classpath "full-path-of-folder-that-contains-resources" -jar jar-to-run.jar
示例: java -classpath C:\\java\\example -jar example.jar

編輯:替代解決方案

<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>

並使用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