我又遇到了一個奇怪的問題,希望這裏有人能夠提供幫助。使用可執行Jar時未找到彈簧引導資源
我有一個彈簧啓動後端模塊,在eclipse中可以正常工作,並且在application.java中啓動main時可以執行應用程序。一切都很好。
我的應用程序使用csv-files將示例數據導入數據庫src/main/resources文件夾中包含的內容。如前所述,在eclipse中啓動一切正常。
現在我想執行它作爲可執行jar,應用程序開始啓動,然後啓動失敗,因爲它無法找到csv文件。打印出來的路徑以及查找文件的位置是正確的,並且csv文件包含在jar中。看起來
模塊的雙龍一樣如下:
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>at.company.bbsng</groupId>
<artifactId>bbsng-import</artifactId>
<version>0.1.0-SNAPSHOT</version>
</parent>
<artifactId>bbsng-import-backend</artifactId>
<name>bbsng-import-backend</name>
<properties>
<start-class>at.company.bbsng.dataimport.Application</start-class>
</properties>
<dependencies>
<!-- SPRING ... -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
<!-- EXCLUDE LOGBACK AND USE LOG4J -->
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-logging</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- COMMONS ... -->
...
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
路徑的CSV文件中屬性格式文件配置如下:
# EXAMPLE PATH
csv.path=config/csv/
Java的配置文件的部分如下:
...
@Value("${csv.path}")
private String csvExamplePath;
@Bean
public Resource addressResource() {
return new ClassPathResource(csvExamplePath + CSV_ADDRESS);
}
...
在JAR文件位於路徑
\config\csv\
堆棧跟蹤:
Caused by: java.io.FileNotFoundException: class path resource [config/csv/Company.csv] cannot be resolved to absolute file path because it does not reside in th
e file system: jar:file:/C:/Development/Projekte/bbsng/trunk/import/backend/target/bbsng-import-backend-0.1.0-SNAPSHOT.jar!/config/csv/Company.csv
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:207)
at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:52)
at at.compax.bbsng.dataimport.app.source.company.CompanyGenerator.init(CompanyGenerator.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java
此外,應用程序的工作,從日食開始時,它如預期,僅可執行的JAR抱怨缺少的CSV文件,什麼都在罐子了。
任何線索都會很棒。
可否請你看看這個,我有類似的問題http://stackoverflow.com/questions/37713335/resource-filenotfoundexception-when-using-executable-jar-command -with-spring-boo – 2016-06-08 22:05:53