2

我在 java 程序中使用 BIRT API。我的代码是:

package com.tecnotree.mdx.product.utils;

import java.util.HashMap;
import java.util.logging.Level;

import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.EngineConstants;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.engine.api.ReportEngine;


public class ExampleReport {

 public static void main(String[] args) {
  // Variables used to control BIRT Engine instance

  ReportEngine eng = null;
  IReportRunnable design = null;
  IRunAndRenderTask task = null;
  HTMLRenderOption renderContext = null;
  HashMap contextMap = null;
  HTMLRenderOption options = null;
  final EngineConfig conf = new EngineConfig();
  conf
    .setEngineHome("C:\\birt-runtime-2_5_2\\birt-runtime-2_5_2\\ReportEngine");
  System.out.println("conf " + conf.getBIRTHome());

  conf.setLogConfig(null, Level.FINE);
  try {
   Platform.startup(conf);
  } catch (BirtException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }


  IReportEngineFactory factory = (IReportEngineFactory) Platform
    .createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
  System.out.println("Factory : " + factory.toString());
  System.out.println(conf.toString());
  IReportEngine engine = factory.createReportEngine(conf);
  System.out.println("Engine : " + engine);

  try {
   design = eng
     .openReportDesign("C:\\birt-runtime-2_5_2\\birt-runtime-2_5_2\\ReportEngine\\samples\\hello_world.rptdesign");
  } catch (Exception e) {
   System.err
     .println("An error occured during the opening of the report file!");
   e.printStackTrace();
   System.exit(-1);
  }
  task = eng.createRunAndRenderTask(design);
  renderContext = new HTMLRenderOption();
  renderContext.setImageDirectory("image");
  contextMap = new HashMap();
  contextMap.put(EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT,
    renderContext);
  task.setAppContext(contextMap);

  options = new HTMLRenderOption();
  options.setOutputFileName("c:/temp/output.html");
  options.setOutputFormat("html");
  task.setRenderOption(options);
  try {
   task.run();
  } catch (Exception e) {
   System.err.println("An error occured while running the report!");
   e.printStackTrace();
   System.exit(-1);
  }
  System.out.println("All went well. Closing program!");
  eng.destroy();

 }
}

但是我在创建报告时面临 NullPointerException。

STACKTRACE :
Exception in thread "main" java.lang.NullPointerException
 at org.eclipse.birt.report.engine.api.impl.ReportEngine$EngineExtensionManager.<init>(ReportEngine.java:784)
 at org.eclipse.birt.report.engine.api.impl.ReportEngine.<init>(ReportEngine.java:109)
 at org.eclipse.birt.report.engine.api.impl.ReportEngineFactory$1.run(ReportEngineFactory.java:18)
 at org.eclipse.birt.report.engine.api.impl.ReportEngineFactory$1.run(ReportEngineFactory.java:1)
 at java.security.AccessController.doPrivileged(Native Method)
 at org.eclipse.birt.report.engine.api.impl.ReportEngineFactory.createReportEngine(ReportEngineFactory.java:14)
 at com.tecnotree.mdx.product.utils.ExampleReport.main(ExampleReport.java:47)

请帮我解决这个问题...我的项目截止日期已到...

感谢您的回复

提前致谢

4

4 回答 4

3

我遇到了类似的问题,并阅读了Report Engine API 文档页面

一些基本的事情是:

  • 放置在您的项目ReportEngine 文件夹中,您可以在 birt-runtime 下载中找到该文件夹​​:这是您处理 .rptdesign 文件并将报告作为输出所需的所有引擎
  • 将 ReportEngine/lib 中的所有 jar 添加到 Java 构建路径
  • 在 ReportEngine/plugins/org.eclipse.birt.report.data.oda.jdbc_xxx 中添加带有驱动程序的必要 jar 以连接到您的数据库(无需将它们添加到 Java 构建路径)

生成 HTML 报告的示例代码是:

import java.util.HashMap;
import java.util.logging.Level;
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.*;

/**This class reads an .rptdesign file, 
 * and gives as output a report in HTML format 
 */
public class BirtEngineDesktop {

    public static void main(String[] args) {
        // Variables used to control BIRT Engine instance
        String reportName = "first_report.rptdesign";
        IReportEngine engine = null;
        IReportRunnable design = null;
        IRunAndRenderTask task = null;
        HTMLRenderOption renderContext = null;
        HashMap contextMap = null;
        HTMLRenderOption options = null;
        final EngineConfig conf = new EngineConfig();
        conf.setEngineHome("ReportEngine");
        System.out.println("BIRT_HOME: " + conf.getBIRTHome());
        conf.setLogConfig(null, Level.FINE);

        try {
            Platform.startup(conf);
        } catch (BirtException e1) {
            e1.printStackTrace();
        }

        IReportEngineFactory factory = (IReportEngineFactory) Platform
                .createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
        System.out.println("Factory: " + factory.toString());
        System.out.println(conf.toString());
        engine = factory.createReportEngine(conf);
        System.out.println("Engine: " + engine);

        try {
            design = engine.openReportDesign("report/" + reportName); //report is needed in this folder
        } catch (Exception e) {
            System.err.println("An error occured during the opening of the report file!");
            e.printStackTrace();
            System.exit(-1);
        }
        task = engine.createRunAndRenderTask(design);
        renderContext = new HTMLRenderOption();
        renderContext.setImageDirectory("images");
        contextMap = new HashMap();
        contextMap.put(EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT,
                renderContext);
        task.setAppContext(contextMap);

        //Set parameter values and validate, if the report requires it
        //task.setParameterValue("Years", 2.0);
        //task.validateParameters();

        options = new HTMLRenderOption();
        options.setOutputFileName("output/" + reportName + ".html"); //output HTML will go to this folder
        options.setOutputFormat("html");
        task.setRenderOption(options);
        try {
            task.run();
        } catch (Exception e) {
            System.err.println("An error occured while running the report!");
            e.printStackTrace();
            System.exit(-1);
        }
        System.out.println("All went well. Closing program!");
        engine.destroy();
        System.exit(0);
    }
}
于 2011-05-16T12:08:27.523 回答
1

不确定这是否会有所帮助,但您的违规行是:

IReportEngine engine = factory.createReportEngine(conf);

我们不再为 BIRT 编写自己的 Web 应用程序(我们使用公司另一部分提供的 Web 应用程序),但通过挖掘我们的旧代码发现我们拥有的和您拥有的之间有两个不同之处。无论其中一个是修复程序,您都必须自己检查。所有关心,没有责任:-)

区别在于:

在设置日志配置引擎主页后但在启动平台之前,我们还设置了一个 HTML 发射器和平台上下文:

HTMLEmitterConfig emitterConfig = new HTMLEmitterConfig();
emitterConfig.setActionHandler (new HTMLActionHandler());
HTMLServerImageHandler imageHandler = new HTMLServerImageHandler();
emitterConfig.setImageHandler (imageHandler);
conf.getEmitterConfigs().put ("html", emitterConfig);

IPlatformContext context = new PlatformServletContext( svrContext );
conf.setPlatformContext( context );

请记住,我们在 servlet 中运行,而不是作为独立应用程序的一部分。所以你可能需要:

IPlatformContext context = new PlatformFileContext( svrContext );

对于平台上下文。试一试,看看它是否有效。不知何故,我怀疑它,因为 PlatformFileContext 是默认值。不过,发射器可能需要考虑。

我可以建议的唯一其他可能性是实际获取BIRT 的源代码(您的特定构建)并查看堆栈跟踪中的违规行。您应该希望能够找出可能导致问题的参数。

例如,最后一行ReportEngine.java:784是以下内容的一部分:

void cacheOpenedDocument( ReportDocumentReader document ) {
    synchronized ( openedDocuments ) {
        LinkedEntry<ReportDocumentReader> entry
            = openedDocuments.add( document );
        document.setEngineCacheEntry( entry ); // << line 784
    }
}

所以几乎可以肯定document传入的是空的。您需要通过各个层来跟踪它,以尝试找出正在发生的事情。

这可能很困难,在这种情况下,您最好提出错误报告并让专家处理它。或者直接找 Jason Weathersby 麻烦,如果你能把你肮脏的小手弄到他的电子邮件地址上 :-)


顺便说一句,您的路径中不需要那些可怕的转义\字符。Java在处理(例如)方面非常好:

conf.setEngineHome("C:/birt-runtime-2_5_2/ReportEngine");
于 2010-04-29T09:43:52.943 回答
0

我知道这个问题已经很老了,但是我今天遇到了这个问题,我花了很长时间才弄清楚原因是什么。原因是我在类路径中包含了一些 birt / eclipse 依赖项。无论出于何种原因,这都会导致这个奇怪的错误。我正在使用 Maven 并包含几个自定义发射器。我的解决方案是在我的自定义发射器 pom 中将我的 Eclipse 依赖项标记为“已提供”。这使所有的 Eclipse 杂物无法进入类路径,而是依靠 BIRT 的 OSGI 加载来处理所有这些。

于 2011-05-31T20:47:31.290 回答
0

我也遇到了这个问题

IReportEngine engine = factory.createReportEngine(conf);
//was null everytime.

我做了所有“蓝色”描述的事情,但错误仍然存​​在。我通过从这里下载并重新安装 ReportEngine 解决了这个问题。

就我而言,我的插件中不存在配置文件夹。将配置文件夹添加到我的插件后,一切都很好。

于 2012-07-11T12:32:21.800 回答