当前位置:网站首页>Jar包启动通过ClassPathResource获取不到文件路径问题
Jar包启动通过ClassPathResource获取不到文件路径问题
2022-08-02 20:26:00 【DanceDonkey】
项目目录结构
maven项目resources目录也是会被编译到classpath下的。
ClassPathResource resource = new ClassPathResource("/map/map.properties");
InputStream inputStream = resource.getInputStream();
System.out.println("inputStream.available() = " + inputStream.available());
System.out.println("resource.getFile().getAbsolutePath() = " + resource.getFile().getAbsolutePath());
如果是使用idea进行本地开发时,是可以获取到classpath下的资源的路径的,因为是读取的磁盘上的真正的那个文件。
但是如果将项目打包成一个jar包后,再获取map.properties是获取不到的,对于操作系统来说最多只能读取到jar包,而不能再继续读取jar包里面的文件了,如果继续读取,只能交给jvm去读了,此时能读取到二进制流,而不能获取到路径。可通过以下几种方式获取classpath下的资源。
ClassPathResource classPathResource = new ClassPathResource("/map/map.properties");
InputStream cis = classPathResource.getInputStream();
//获取classpath下第一个/map/map.properties,这个根据jar包的加载先后顺序有关
InputStream ris = ClassPathResource.class.getClassLoader().getResourceAsStream("/map/map.properties");
//这个是获取classpath下所有的map/map.properties,在jvm运行时,所有jar都相当于是一个classpath
Enumeration<URL> resources = ClassPathResource.class.getClassLoader().getResources("/map/map.properties");
边栏推荐
猜你喜欢
人尽皆知的云原生,到底是大势所趋还是过度炒作?
信息学奥赛一本通(1259:【例9.3】求最长不下降序列)
Tencent YunMeng every jie: I experienced by cloud native authors efficiency best practices case
X 2 Earn必须依靠旁氏启动?GameFi的出路在哪?(下)
Digital twins help visualize the construction of smart cities
"A daily practice, happy water problem" 1374. Generate a string with an odd number of each character
信息学奥赛一本通(1258:【例9.2】数字金字塔)
HCIP--路由策略实验
SQL基础练习题(mysql)
Common tools and test methods for interface testing (Introduction)
随机推荐
Golang source code analysis: juju/ratelimit
Solve the docker mysql can't write Chinese
广东省数字经济发展指引 1.0之建成数据安全保障体系
go——内存分配机制
接口测试常用工具及测试方法(入门篇)
js如何获取浏览器缩放比例
如何使用windbg查看C#某个线程的栈大小 ?
Digital twins help visualize the construction of smart cities
setup syntax sugar defineProps defineEmits defineExpose
封装和包、访问修饰权限
postgresql autovaccum自动清理
golang source code analysis: uber-go/ratelimit
汉源高科千兆4光4电工业级网管型智能环网冗余以太网交换机防浪涌防雷导轨式安装
V - memo new instructions
TodoList案例
C# Barrier类
Linphone 被叫方如何解析来电SIP消息中的自定义头消息
.NET如何快速比较两个byte数组是否相等
C语言中变量在内存中的保存与访问
Implement fashion_minst clothing image classification