Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在可执行jar文件中加载java中的文件路径失败_Java_Jasper Reports - Fatal编程技术网

在可执行jar文件中加载java中的文件路径失败

在可执行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

我正在用java编写一个带有netbeans IDE的程序,该程序接收jasper报告文件并将其显示给用户。 因此,我编写了以下代码:

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