jar执行过程中的资源路径更改 [英] Resource path change during jar execution

查看:62
本文介绍了jar执行过程中的资源路径更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取Resources目录中的json文件,并且使用以下内容:

I'm trying to read a json file in a Resources directory and I use the following:

jsonObject = this.readJson(this.getClass().getClassLoader().getResource("jsonFileName").getPath());

在IDE中,它可以正常运行,但是当我构建thw jar并尝试通过java -jar jarName运行它时,出现找不到文件"错误,并且当我检查路径时,它看起来像这样:

In the IDE it runs correctly but when I build thw jar and try to run it by java -jar jarName I get a "File not found" Error and when I checked the path, it looks like this:

...projectName/target/projectName-1.0-SNAPSHOT.jar!/kb/is/identity.json

在IDE上运行时,路径如下:

When running on the IDE the paths looks like this:

...projectName/target/classes/kb/is/identity.json

推荐答案

使用getResourceAsStream而不是getResource很重要. 看看 getClassLoader().getResourceAsStream()如何在Java中工作

It's important to use getResourceAsStream, not getResource. Have a look at How getClassLoader().getResourceAsStream() works in java

这篇关于jar执行过程中的资源路径更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆