English 中文(简体)
Jasper 没有在 jar 文件中工作
原标题:jasper is not working in jar file

我在桌面应用程序中使用了贾斯珀的报告

计为

    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

 }

当检查控制台时, 它说文件没有找到, 所以我担心为什么会发生这个错误,

现在我得到

C:UsersFFIS WIN03dist
eport32name_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 文件作为资源, 这样您就可以使用 < code> get ReresourcAsStream 来阅读它, 甚至从一个罐子文件中读取。 但是, 您不应该在文件系统中使用它作为 < code> File 。 事实上, 它使用了应用程序启动目录, 甚至不同 。

要检测是否存在: getResource 将返回一个无效的 URL 。 这可能应该在 < em> 之前 < em > 进行 < code> get ResourceAsStream 。

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

需要注意的是,get ReresourcAsStream 摘录来自您的“强势”类人称路径 new file 与您当前目录是相对的。

如果您的 Netbeans 类资源包括: ${currentdir}/building/ classes , 您的jrxml 文件存在, 那么 get ResourcAsStream 将会成功, 但 new file 将会因 ${currentdir}/ Sirform Export.jrxml 不存在而失败 。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签