2012-05-27 37 views
0

我用碧玉在我的桌面應用程序報告,碧玉不jar文件工作

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 

回答

0

該文件中不存在的類路徑。通常,在許多情況下,從命令行獲取文件的絕對路徑會更好。否則,請確保您的類路徑中包含該文件的位置。

+0

文件與其他類存在於同一類中 – Ali

+0

如何從命令行運行此命令? –

+0

是的,我嘗試從兩個命令行 – Ali

0

您希望將.jrxml文件作爲資源,因此即使是從jar文件也可以使用getResourceAsStream來讀取它。但是,你不應該在文件系統中使用它作爲File。事實上,使用應用程序的啓動目錄,甚至有所不同。

要檢測存在:getResource不存在時將返回空URL。這應該在getResourceAsStream之前完成

您使用getResourceAsStream的類的包將成爲相對路徑的起始路徑。您可以使用絕對路徑,如「/org/jasperreports/samples/sample1.jrxml」。

+0

請參閱編輯 – Ali

+0

對不起沒有經驗的動態編譯在堆棧跟蹤上說些什麼。預編譯報告。試試看,也許有些東西沒有很好的定義。 –

0

需要注意的是,getResourceAsStream類路徑中提取,但new File與您當前目錄相對。

如果從NetBeans中的資源類包括:${currentdir}/build/classes,並且您的JRXML文件是存在的,那麼getResourceAsStream會成功,但作爲$ {} currentdir是/OriginalExport.jrxml不存在new File將失敗。