Jasper Report无法找到类路径

时间:2015-04-16 01:55:40

标签: java jasper-reports

我是jasper报道的新手。我正在使用JasperReport最新版本6.0.3,所以我有问题加载我创建的报告来打印账单。然后我收到如下错误信息,

net.sf.jasperreports.engine.JRException: java.io.FileNotFoundException: I:\Projects\Testsrc\com\thus\test\Blank_A4_Landscape.jrxml (The system cannot find the path specified)
    at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:221)
    at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:192)
    at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:183)
    at com.thus.test.Test.main(Test.java:29)
Caused by: java.io.FileNotFoundException: I:\Projects\Testsrc\com\thus\test\Blank_A4_Landscape.jrxml (The system cannot find the path specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:216)

这是我的示例代码;

try {
        connection = getConnection("jdbc:sqlserver://xxx.xxx.xx.xx:1433;DatabaseName=Thusya");

        JasperDesign jasperDesign = JRXmlLoader.load(new File("")
                .getAbsolutePath()
                + "src/com/thus/test/Blank_A4_Landscape.jrxml");
        String sqlString = "SELECT * FROM TABLE1 WHERE CNO = '156801'";
        JRDesignQuery designQuery = new JRDesignQuery();
        designQuery.setText(sqlString);
        jasperDesign.setQuery(designQuery);

        JasperReport jasperReport = JasperCompileManager
                .compileReport(jasperDesign);
        JasperPrint jasperPrint = JasperFillManager.fillReport(
                jasperReport, null, connection);

        JasperViewer.viewReport(jasperPrint);

    } catch (JRException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

以前的Stacks没有解决我的问题,任何人都可以指导我继续前进????

2 个答案:

答案 0 :(得分:2)

您没有在src之前添加/。这就是你收到错误的原因。

JasperDesign jasperDesign = JRXmlLoader.load(new File("")
            .getAbsolutePath()
            + "/src/com/thus/test/Blank_A4_Landscape.jrxml");

希望它会对你有所帮助。

答案 1 :(得分:0)

这个帮助对我有帮助,

JasperDesign jasperDesign = JRXmlLoader.load(new File(
    "C:/New folder/Blank_A4_Landscape.jrxml"));

试试这个,告诉我。