繁体   English   中英

jasper 在 jar 文件中不起作用

[英]jasper is not working in jar file

我在桌面应用程序中使用了 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

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

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

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

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

需要注意的是getResourceAsStream从您的classpath 中提取,但new File与您的当前目录相关。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM