无法通过 JRXmlLoader 加载位于 jar 文件中的 jrxml:获取 java.io.FileNotFoundException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20626140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:12:18  来源:igfitidea点击:

Can't load jrxml located in jar file via JRXmlLoader: getting java.io.FileNotFoundException

javajasper-reports

提问by

I' m using JasperReportsin my Javaapplication.

我在Java应用程序中使用JasperReports

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.

我有一个名为“reports”的包来存储所有生成的报告。这是我在我的应用程序中调用我的 jasper 报告的方式。

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)

It is not suitable to give the full path to the JRXmlLoaderbecause 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

不适合给出JRXmlLoader的完整路径,因为如果您要在其他计算机上运行此应用程序,则必须根据计算机路径更改所有编码。所以帮我解决这个问题

采纳答案by MadProgrammer

/reports/teacherPay.jrxmlis an absolute file path, meaning, go to the root of the current drive and find the file teacherPay.jrxmlin the reportsdirectory...

/reports/teacherPay.jrxml是一个绝对文件路径,意思是,去当前驱动器的根目录,找到目录teacherPay.jrxml中的reports文件...

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

相反,尝试将报告作为资源加载(鉴于您声明它位于 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");

Now, having said that. Unless you are making dynamic changes at runtime, you shouldn't be loading the jrxmlfile, but instead, should have pre-compiled the file and should be loading the .jasperfile instead. This will be faster and generally less error prone...

现在,话虽如此。除非您在运行时进行动态更改,否则您不应该加载jrxml文件,而是应该预编译文件并加载.jasper文件。这将更快,通常更不容易出错......

回答by Thusitha Wickramasinghe

JasperDesign jd  = JRXmlLoader.load(getClass().getResource("/reports/teacherPay.jrxml"));

This is not working some time, because getResource()returns URL. If your file path contains " "it returns "%20"Like this

这在一段时间内不起作用,因为getResource()返回URL. 如果您的文件路径包含" "它返回"%20"这样

"C:\Users\Sandaru Weerathunga\Desktop\Dasatha Institute\src\reports\teacherPay.jrxml"

returns

返回

"C:\Users\Sandaru%20Weerathunga\Desktop\Dasatha%20Institute\src\reports\teacherPay.jrxml"

In that matter you can use getResourceAsStream()method that retuns InputStream. Try this, This works for me.

在这种情况下,您可以使用getResourceAsStream()retuns 的方法InputStream。试试这个,这对我有用。

JasperReport jp = JasperCompileManager.compileReport(getClass().getResourceAsStream("/reports/teacherPay.jrxml"));