无法通过JRXmlLoader加载位于jar文件中的jrxml:获取java.io.FileNotFoundException [英] Can't load jrxml located in jar file via JRXmlLoader: getting java.io.FileNotFoundException
问题描述
我在 Java 应用程序中使用 JasperReports 。
I' m using JasperReports in my Java application.
我有一个名为reports的软件包来存储生成的所有报告。这是我在我的应用程序中调用我的jasper报告的方式。
I have a package named "reports" to store all the reports generated. Here is the way I'm calling my jasper report in my application.
JasperDesign jd = JRXmlLoader.load("C:\\Users\\Sandaru Weerathunga\\Desktop\\Dasatha Institute\\src\\reports\\teacherPay.jrxml");
这是有效的。
我试过而不是给出完整的路径:
This is working.
Instead of giving the full path , I tried:
JasperDesign jd = JRXmlLoader.load("/reports/teacherPay.jrxml");
但这在运行程序时显示错误:
But this is showing an Error while running the program:
net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException:
/reports/teacherPay.jrxml (The system cannot find the path specified)
at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:176)
at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:156)
不适合提供 JRXmlLoader 的完整路径,因为如果你要在其他计算机上运行此应用程序,您必须根据计算机路径更改所有编码。所以帮助我吧
It is not suitable to give the full path to the JRXmlLoader because if you are going to run this application in other computer you have to change all the coding according to the computer path. So help me on this
推荐答案
/reports/teacherPay.jrxml
是绝对文件路径,意思是,转到当前驱动器的根目录,并在报告
teacherPay.jrxml >目录......
/reports/teacherPay.jrxml
is an absolute file path, meaning, go to the root of the current drive and find the file teacherPay.jrxml
in the reports
directory...
如果我正确地读出你的问题,那就不是你想要的那样
Which, if I read your question correctly, isn't what you want
相反,尝试将报告加载为资源(假设您声明它在包中
Instead, try loading the report as a resource (given the fact that you state that it's within a package
JasperDesign jd = JRXmlLoader.load(getClass().getResource("/reports/teacherPay.jrxml"));
如果报告未打包在您的应用程序上下文中,那么您将需要使用相对路径,例如。
If the report isn't packaged within your application context, then you will need to use a relative path instead, for example.
JasperDesign jd = JRXmlLoader.load("reports/teacherPay.jrxml");
现在,已经说过了。除非你在运行时进行动态更改,否则你不应该加载 jrxml
文件,但是应该预先编译文件,而应该加载 .jasper
文件。这将更快,通常不易出错......
Now, having said that. Unless you are making dynamic changes at runtime, you shouldn't be loading the jrxml
file, but instead, should have pre-compiled the file and should be loading the .jasper
file instead. This will be faster and generally less error prone...
这篇关于无法通过JRXmlLoader加载位于jar文件中的jrxml:获取java.io.FileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!