访问 jar 文件中的报告时出现问题
我有一个包含多个报告(文件 .jasper
)的 jar 应用程序,我获取报告路径的方式是:
getClass().getResource("/reportes/mireporte.jasper").toString();
但是当我执行时,出现下一个错误:FileNotFoundException 尽管当我用 winrar 打开报告时,报告位于 jar 文件中。
我的问题是:
- 这是获取 jar 文件中报告路径的方法吗?
- 是否可以打开 jar 文件中的报告?
I have a jar application that contains several reports (files .jasper
) and the way that I get the path of the report is:
getClass().getResource("/reportes/mireporte.jasper").toString();
but when I execute I get the next error: FileNotFoundException
although the report is within the jar file when I open it with the winrar.
My questions are:
- Is this the way to get the path of a report in a jar files?
- Is it possible to open reports that are within of a jar file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么要将资源转换为字符串?
尝试使用(取决于您想要如何使用资源):
getResource("/report/mireporte.jasper").getFile()
getResource("/report/mireporte.jasper").getInputStream()
从“getResource”返回的对象是 URL:
Why are you converting the resource to String ??
Try to use (depending on how you want to use the resource):
getResource("/report/mireporte.jasper").getFile()
getResource("/report/mireporte.jasper").getInputStream()
Returned object from 'getResource' is URL:
是的,调用
getClass().getClassLoader().getResources("mireporte.jasper")
- 它会给您一个与名称匹配的资源列表。是的!使用
getClass().getResourceAsStream("...")
并从中读取!例子:
Yes, call the
getClass().getClassLoader().getResources("mireporte.jasper")
- it will give you a list of resources that matches the name.Yes! Use
getClass().getResourceAsStream("...")
and read from that one!Example: