我在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)
请帮助我......我的项目截止日期已经达到......
感谢您的回复
先谢谢
答案 0 :(得分:3)
我遇到了类似的问题并且没有阅读Report Engine API documentation page。
一些基本的东西是:
生成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);
}
}
答案 1 :(得分: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是默认值。但是发射器可能需要考虑。
我可以建议的唯一其他可能性是实际获取source code for BIRT (your particular build)并查看堆栈跟踪中的违规行。您应该能够找出可能导致问题的参数。
例如,最后一行ReportEngine.java:784
属于:
void cacheOpenedDocument( ReportDocumentReader document ) {
synchronized ( openedDocuments ) {
LinkedEntry<ReportDocumentReader> entry
= openedDocuments.add( document );
document.setEngineCacheEntry( entry ); // << line 784
}
}
因此几乎可以肯定传入的document
为空。您需要通过各个层次来回过头来尝试找出正在发生的事情。
这可能很难,在这种情况下,你可能会更好,raising a bug report并让专家处理它。或者直接讨论Jason Weathersby,如果你能在他的电子邮件地址上得到你的肮脏的小手: - )
顺便说一句,您不需要路径中那些可怕的转义\
个字符。 Java在处理(例如)时完全没问题:
conf.setEngineHome("C:/birt-runtime-2_5_2/ReportEngine");
答案 2 :(得分:0)
我知道这个问题已经很老了,但我今天遇到了这个问题,我花了很长时间才弄明白原因是什么。原因是我在我的类路径中包含了一些birt / eclipse依赖项。无论出于何种原因,这会导致这种奇怪的错误我正在使用maven并包含几个自定义发射器。我的解决方案是在我的自定义发射器poms中将我的eclipse依赖项标记为“提供”。这使得所有的日食都不会进入类路径,而是依靠BIRT的OSGI加载来处理所有这些。
答案 3 :(得分:0)
我也遇到了这个问题
IReportEngine engine = factory.createReportEngine(conf);
//was null everytime.
我做了所有“蓝色”的描述,但错误仍然存在。 我通过从here.
下载并重新安装ReportEngine解决了这个问题就我而言,我的插件中没有配置文件夹。将配置文件夹添加到我的插件后,一切都很好。