如何从jar中添加和读取资源文件 [英] how to add and read resource file from jar

查看:24
本文介绍了如何从jar中添加和读取资源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在java程序读取的路径中有hibernate.cfg.xml和test.txt.现在,当我使用 maven 创建 jar 时,这些文件不存在.所以我读到我应该放在资源文件夹中,所以现在我的目录结构是

I have hibernate.cfg.xml and test.txt in the path which i read by java program. Now when i created the jar using maven those files were not present. So i read that i should put in the resources folder , so now my directory structure is

scr -> main-> java

scr -> main-> java

        ->resources

现在我可以看到 jar 中的文件,但它们不在资源文件夹中它基本上

Now i can see the files in the jar but they are not inside resource folder it bascically

myjar.jar -> com(源代码)

myjar.jar -> com (source code)

       -> META -INF
       -> hibernate.cfg.xml
       -> test.txt

我尝试使用

getClass().getResourceAsStream("test.txt")

getClass().getResourceAsStream("test.txt")

但是得到了 null..

but got null..

让我知道哪些步骤是错误的?

Let me know what steps are wrong ?

推荐答案

这篇文章getClass().getResourceAsStream("/test.txt") 有什么不同吗?

这篇关于如何从jar中添加和读取资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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