jasper在jar文件中不起作用

时间:2012-05-27 17:29:05

标签: java filesystems jasper-reports

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

as

    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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

要检测是否存在:getResource将在不存在时返回空URL。这应该在 getResourceAsStream之前完成。

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

答案 2 :(得分:0)

需要注意的是getResourceAsStream类路径中提取,但new File与当前目录相关。

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