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

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

问题描述

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


scr - > main-> java

   - >资源




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


myjar.jar - > com(源代码)

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


我尝试使用

$ b进行访问
$ b

getClass()。getResourceAsStream(test.txt)



但是空了..



让我知道哪些步骤有误?

解决方案

查看这篇文章 getClass()。getResourceAsStream(/ test.txt)做出改变?


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

        ->resources

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

myjar.jar -> com (source code)

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

I tried accessing using

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

but got null..

Let me know what steps are wrong ?

解决方案

Looking at this article, Does getClass().getResourceAsStream("/test.txt") make a difference?

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

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