无法从类路径

时间:2018-07-13 09:32:28

标签: java spring

我正在使用 java -classpath“ file_name.properties” -jar file_name.jar运行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)

所以请帮助我解决此问题。

谢谢, 纳西

2 个答案:

答案 0 :(得分:0)

您尝试过没有列表标签吗?​​

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

答案 1 :(得分:0)

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