原文链接:https://blog.csdn.net/qq_18748427/article/details/78606432

springboot打成jar后获取classpath下文件失败

使用如下代码:

ClassPathResource resource = new ClassPathResource("application.yml");
File file = resource.getFile();
FileUtils.readLines(file).forEach(System.out::println);

未打包时可以获取到文件,打包后报错

Caused by: java.io.FileNotFoundException: class path resource [application.yml] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/D:/sunmnet/JetBrains/workspace/bigdata-parse-table/target/bigdata-parse-table-1.0-SNAPSHOT.jar!/BOOT-INF/classes!/application.yml
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:215) ~[spring-core-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:53) ~[spring-core-4.3.12.RELEASE.jar!/:4.3.12.RELEASE]
at hello.whz.Application.lambda$lookup$0(Application.java:30) [classes!/:1.0-SNAPSHOT]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:732) [spring-boot-1.5.8.RELEASE.jar!/:1.5.8.RELEASE]
... 14 common frames omitted

这是因为打包后Spring试图访问文件系统路径,但无法访问JAR中的路径。
因此必须使用resource.getInputStream()

ClassPathResource resource = new ClassPathResource("application.yml");
InputStream inputStream = resource.getInputStream();
IOUtils.readLines(inputStream).forEach(System.out::println);

springboot打成jar后获取classpath下文件失败的更多相关文章

  1. java 获取classpath下文件多种方式

    java 获取classpath下文件多种方式 一:properties下配置 在resources下定义server.properties register.jks.path=classpath\: ...

  2. springboot打成jar后文件读取问题

    springboot打成的jar包里面不能用File去获取文件对象,只能用流的方式去读取. 获取方式: InputStream resourceAsStream  = 类名.class.getClas ...

  3. SpringBoot 项目打包后获取不到resource下资源的解决

    SpringBoot 项目打包后获取不到resource下资源的解决 在项目中有几个文件需要下载,然后不想暴露真实路径,又没有CDN,便决定使用接口的方式来获取文件.最初的时候使用了传统的方法来获取文 ...

  4. springboot打成jar包后无法解压

    springboot打成jar包后无法解压 Springboot打出来的jar,用压缩工具解压报错.Why? 先说解决办法. 1.解决办法 executable属性导致的,属性改成false后重新打包 ...

  5. springboot打成jar包和war包的两种方式,并引入第三方jar包!

    springboot打成jar包和war包的两种方式,并引入第三方jar包! 首先把需要引入的第三方jar包引入到本地项目中,在引用的模块下加一个lib文件夹 一.打成jar包 1.修改pom文件里的 ...

  6. C# 获取目录下文件

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  7. springboot-项目获取resources下文件的方法

    spring项目获取resources下文件的方法   最近写读取模板文件做一些后续的处理,将文件放在了项目的resources 下,发现了一个好用的读取方法:   比如上边是你需要读取的文件:  读 ...

  8. SpringBoot读取Linux服务器某路径下文件\读取项目Resource下文件

    // SpringBoot读取Linux服务器某路径下文件 public String messageToRouted() { File file = null; try { file = Resou ...

  9. spring java 获取webapp下文件路径

    spring java 获取webapp下文件路径 @RequestMapping("/act/worldcup_schedule_time/imgdownload") @Resp ...

随机推荐

  1. iOS 关于NSNotificationCenter

    通常我们在 iOS 中发生什么事件时该做什么是由 Delegate 实现的,  Apple 还为我们提供了另一种通知响应方式,那就是 NSNotification. NSNotificationCen ...

  2. 2 《锋利的jQuery》jQuery选择器

    tip1:jquery检查某个元素是否存在:if($("#tt").length>0){}或者if($("#tt")[0]){} 先说css选择器有: 标 ...

  3. struts2 codebehind + actionPackages 实现Action零配置

    1.jar包:struts2-codebehind-plugin-2.2.1.1.jar 2.struts.xml:<!-- codebehind中查找action的返回结果资源时的默认文件夹 ...

  4. Sturs2 -概念讲解 第一弹

    源码下载地址:http://struts.apache.org/ struts-2.5.14.1-all.zip --所有内容 struts-2.5.14.1-apps.zip  --实例的应用 st ...

  5. 线程池ThreadPool的常用方法介绍

    线程池ThreadPool的常用方法介绍 如果您理解了线程池目的及优点后,让我们温故下线程池的常用的几个方法: 1. public static Boolean QueueUserWorkItem(W ...

  6. [acm]HDOJ 3082 Simplify The Circuit

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3082 字符串处理+并联电阻公式 //11481261 2014-08-18 16:52:47 Acc ...

  7. multitail

    multitail 在分隔的窗口查看你的日志

  8. BZOJ-3626:LCA(离线+树链剖分)

    Description 给出一个n个节点的有根树(编号为0到n-1,根节点为0).一个点的深度定义为这个节点到根的距离+1.设dep[i]表示点i的深度,LCA(i,j)表示i与j的最近公共祖先.有q ...

  9. ACM学习历程—FZU 2144 Shooting Game(计算几何 && 贪心 && 排序)

    Description Fat brother and Maze are playing a kind of special (hentai) game in the playground. (May ...

  10. 变废为宝,用旧电脑自己DIY组建 NAS 服务器

    i17986 出品,必属佳作! 前言: 老外不喜欢升级硬件和软件,大家应该都知道.我昨天无意看到 FreeNAS 自述文件,这个系统可以让你使用旧的计算机硬件,于是我决定这么做.垃圾电脑你怎么能没有, ...