0

我在我的桌面应用程序中使用了 Jasper 报告,

作为

    void showResports(){
    try{
      // OriginalExportOriginalExport 
         String reportName ="";
        //ExportReport4.jasperExportReport4.jasper
        if(export.equalsIgnoreCase("1")){
  reportName = "OriginalExport.jrxml";
        }
        else if(export.equalsIgnoreCase("0")){
         reportName = "ExportReport4.jrxml";

        }


   InputStream in =getClass().getResourceAsStream(reportName);

  if(in==null|| connection==null) return ;


   JasperDesign jasperDesign = JRXmlLoader.load(in);
   JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
   JasperPrint jp = JasperFillManager.fillReport(jasperReport, null, connection);
  System.out.println("Object of Jasper Print created");

              JRViewer jv = new JRViewer(jp);

     //Insert viewer to a JFrame to make it showable
            JFrame jf = new JFrame();
            jf.getContentPane().add(jv);
            jf.validate();
            jf.setVisible(true);
            jf.setSize(new Dimension(800,600));
            jf.setLocation(300,100);
            jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);



  File f=new File(reportName);
  if(f.exists()) System.out.println("Yes Yes File is there");
  else 
  System.out.println("File is not there"+f.getAbsolutePath());


   }catch(Exception e){
    e.printStackTrace();}

    }// showReports

 }

在控制台上检查时它说找不到文件,所以我很担心为什么会发生这个错误,尽管它在 netebeans 中运行良好?

现在我明白了

C:\Users\FFIS WIN03\dist\report32name_1338142442500_473143.java
        at net.sf.jasperreports.engine.design.JRJavacCompiler.compileClasses(JRJavacCompiler.java:89)
        at net.sf.jasperreports.engine.design.JRAbstractClassCompiler.compileUnits(JRAbstractClassCompiler.java:63)
        at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:188)
        at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:212)
        at pos.ViewAllPayments$Reporter.showResports(ViewAllPayments.java:1339)
        at pos.ViewAllPayments.importButtonActionPerformed(ViewAllPayments.java:1098)
        at pos.ViewAllPayments.access$2100(ViewAllPayments.java:39)
        at pos.ViewAllPayments$20.actionPerformed(ViewAllPayments.java:448)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$000(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.io.IOException: Cannot run program "javac": CreateProcess error= 2, The system cannot find the file specified
        at java.lang.ProcessBuilder.start(Unknown Source)
        at java.lang.Runtime.exec(Unknown Source)
        at java.lang.Runtime.exec(Unknown Source)
        at net.sf.jasperreports.engine.design.JRJavacCompiler.compileClasses(JRJavacCompiler.java:58)
        ... 43 more
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
        at java.lang.ProcessImpl.create(Native Method)
        at java.lang.ProcessImpl.<init>(Unknown Source)
        at java.lang.ProcessImpl.start(Unknown Source)
        ... 47 more
4

3 回答 3

0

需要注意的是,它从您的classpathgetResourceAsStream中提取,但与您当前的目录相对。new File

如果您来自 Netbeans 的类资源包括:${currentdir}/build/classes,并且您的 jrxml 文件在那里,那么getResourceAsStream将成功,但new File由于 ${currentdir}/OriginalExport.jrxml 不存在而将失败。

于 2012-05-27T20:33:59.130 回答
0

您希望将 .jrxml 文件作为资源,因此您getResourceAsStream甚至可以从 jar 文件中读取它。但是你不应该像File在文件系统中那样使用它。事实上,使用的应用程序启动目录,甚至会有所不同。

检测存在:getResource当不存在时将返回一个空 URL。那可能应该在之前 getResourceAsStream完成。

您使用的类的包getResourceAsStream将是相对路径的起始路径。您可以使用像“/org/jasperreports/samples/sample1.jrxml”这样的绝对路径。

于 2012-05-27T18:11:47.997 回答
0

该文件在类路径中不存在。通常,在许多情况下,从命令行获取文件的绝对路径会更好。否则,请确保您的类路径中有文件的位置。

于 2012-05-27T17:32:12.257 回答