在可执行jar文件中加载java中的文件路径失败
我正在用java编写一个带有netbeans IDE的程序,该程序接收jasper报告文件并将其显示给用户。 因此,我编写了以下代码:在可执行jar文件中加载java中的文件路径失败,java,jasper-reports,Java,Jasper Reports,我正在用java编写一个带有netbeans IDE的程序,该程序接收jasper报告文件并将其显示给用户。 因此,我编写了以下代码: String reportSource = null; try { URL url = getClass().getResource("/mypkg/resources/jreports/report.jrxml"); reportSource = url.getPath(); reportSource = reportSource.re
String reportSource = null;
try {
URL url = getClass().getResource("/mypkg/resources/jreports/report.jrxml");
reportSource = url.getPath();
reportSource = reportSource.replaceFirst("/", "");
System.out.println(reportSource);
} catch (Exception ex) {}
try{
JasperDesign jasperDesign = JRXmlLoader.load(reportSource);
JasperReport jasperReport =
JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint =
JasperFillManager.fillReport(jasperReport, null, getConnection());
JasperViewer.viewReport(jasperPrint, false);
}catch(Exception ex){}
我的问题是,当我在IDE中运行我的程序时,它运行得很好,但是当我尝试从dist文件夹运行jar文件时,它给了我以下异常
java.io.FileNotFoundException
问题出在哪里?你确定你把文件打包在罐子里了吗?而且
URL
和文件不一定是一回事,嵌入式资源不能被视为文件
是的,我把它放在/mypkg/resources/jreports/report.jrxml那么解决方案是什么呢?jrxmloader.load(String)
正在寻找一个“文件”,相反,您可能需要使用jrxmloader.load(InputStream)
,而不是使用Class#getResourceAsStream