【发布时间】:2014-11-28 21:03:27
【问题描述】:
我再次遇到一个奇怪的问题,希望这里有人可以提供帮助。
我有一个 spring boot 后端模块,在 eclipse 中运行良好,并且在 application.java 中启动 main 时应用程序是可执行的。一切都很好。
我的应用程序使用 src/main/resources 文件夹中包含的 csv 文件将示例数据导入数据库。如前所述,在 Eclipse 中启动时一切正常。
现在我想将它作为可执行 jar 执行,应用程序开始启动,然后无法启动,因为它找不到 csv 文件。它打印出来的路径、查找文件的位置是正确的,并且 csv 文件在包含的 jar 中。
模块的 Pom 如下所示:
<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
再次,应用程序从 eclipse 启动时按预期工作,只有可执行 jar 抱怨缺少 csv 文件,jar 中已经存在什么。
任何线索都会很棒。
【问题讨论】:
标签: java resources spring-boot executable-jar filenotfoundexception