[英]Accessing report inside a .jar file
So guys, as what the title implies. 各位,正如标题所暗示的。 I cant access my report when i clean and build my program to a jar file to be deployed.
当我清理程序并将其构建为要部署的jar文件时,无法访问报告。 I tried surfing the internet for possible solutions but none of them has gone right.
我尝试在互联网上浏览以寻求可能的解决方案,但是没有一个能解决问题。 So below is what i did to try to access the report inside the jar file:
所以下面是我尝试访问jar文件中的报告的操作:
URL reportURL = getClass().getClassLoader().getResource("assessmentrecordsystem\\reports\\report1.jrxml");//returns nothing
reportInputStream = this.getClass().getClassLoader().getResourceAsStream("\\reports\\report1.jrxml");//all so returns nothing
What did i miss?? 我错过了什么??
This is what my jar file contains: 这是我的jar文件包含的内容:
Considering a structure like this: 考虑这样的结构:
myjar.jar
|__ assessmentrecordsystem
|__ MyClass.class
|__ reports
|__ report1.jrxml
You can load the resource with a relative path from your MyClass with getResourceAsStream() 您可以使用getResourceAsStream()从MyClass加载具有相对路径的资源。
InputStream is = MyClass.class.getResourceAsStream("reports/report1.jrxml");
Otherwise, you need to add the following entry to the /META-INF/MANIFEST.MF
file inside your jar: 否则,您需要将以下条目添加到jar内的
/META-INF/MANIFEST.MF
文件中:
Class-Path: .
Then you can load your file with an absolute path from the jar root with getClassLoader().getResourceAsStream() 然后,您可以使用getClassLoader()。getResourceAsStream()从jar根目录加载具有绝对路径的文件。
InputStream is = MyClass.class.getClassLoader().getResourceAsStream(
"/assessmentrecordsystem/reports/report1.jrxml");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.