相关疑难解决方法(0)

运行jar时找不到类路径资源

在Spring Boot 1.1.5和1.1.6中都有这个问题 - 我正在使用@Value注释加载一个类路径资源,当我在STS(3.6.0,Windows)中运行应用程序时,它可以正常工作.但是,当我运行mvn包然后尝试运行jar时,我得到FileNotFound异常.

资源message.txt位于src/main/resources中.我检查了jar并验证它在顶层包含文件"message.txt"(与application.properties相同的级别).

这是应用程序:

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application implements CommandLineRunner {

    private static final Logger logger = Logger.getLogger(Application.class);

    @Value("${message.file}")
    private Resource messageResource;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... arg0) throws Exception {
        // both of these work when running as Spring boot app from STS, but
        // fail after mvn package, and then running as java -jar
        testResource(new ClassPathResource("message.txt"));
        testResource(this.messageResource);
    }

    private void testResource(Resource resource) {
        try { …
Run Code Online (Sandbox Code Playgroud)

java spring-boot

106
推荐指数
5
解决办法
11万
查看次数

标签 统计

java ×1

spring-boot ×1