Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Simple Execute (BIRT) 2.1

< To: Integration Examples (BIRT)

Integrating BIRT in a Simple Java Application

This example demonstrates using the Report Engine API (RE API) to run and render a report design to HTML.

Add comments at the bottom of the example.

BIRT Report Engine API: Return to the BIRT Report Engine API examples

BIRT Version

BIRT 2.1.1

Source

ExecuteReport.java

import java.util.HashMap;
import java.util.logging.Level;
 
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.EngineException;
import org.eclipse.birt.report.engine.api.HTMLActionHandler;
import org.eclipse.birt.report.engine.api.HTMLEmitterConfig;
import org.eclipse.birt.report.engine.api.HTMLRenderContext;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.HTMLServerImageHandler;
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;
 
public class ExecuteReport {
    static void executeReport() throws EngineException {
        final HashMap<String, Integer> PARAMETERS = new HashMap<String, Integer>();
 
        final String NAME = "Top Count";
        final Integer VALUE = new Integer(4);
 
        PARAMETERS.put(NAME, VALUE);
 
        IReportEngine engine = null;
        EngineConfig config = null;
 
        try {
 
           // Configure the Engine and start the Platform
           config = new EngineConfig();
           config.setEngineHome("C:/birt-runtime-2_1_1/birt-runtime-2_1_1/ReportEngine");
           // set log config using (null, Level) if you do not want a log file
           config.setLogConfig("C:/birt/logs", Level.FINE);
 
           Platform.startup(config);
           final IReportEngineFactory FACTORY = (IReportEngineFactory) Platform.
                 createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
 
           engine = FACTORY.createReportEngine(config);
           engine.changeLogLevel(Level.WARNING);
 
        } catch(Exception ex) {
           ex.printStackTrace();
        }
 
        // Configure the emitter to handle actions and images
        final HTMLEmitterConfig EMITTER_CONFIGURATION = new HTMLEmitterConfig();
 
        EMITTER_CONFIGURATION.setActionHandler(new HTMLActionHandler());
        HTMLServerImageHandler imageHandler = new HTMLServerImageHandler();
        EMITTER_CONFIGURATION.setImageHandler(imageHandler);
        config.getEmitterConfigs().put("html", EMITTER_CONFIGURATION); // $NON-NLS-1$
 
        // Open the report design
        final IReportRunnable DESIGN =
              engine.openReportDesign("C:/test/2.1/executereport/test.rptdesign"); 
 
        // Create task to run and render the report:
        final IRunAndRenderTask TASK = engine.createRunAndRenderTask(DESIGN); 
 
        // Set Render context to handle URL and image locations
        final HTMLRenderContext RENDER_CONTEXT = new HTMLRenderContext();
        // Set the Base URL for all actions
        RENDER_CONTEXT.setBaseURL(<nowiki>"http:// localhost/"</nowiki>);
        // Tell the Engine to prepend all images with this URL - Note this requires using 
        // the HTMLServerImageHandler
        RENDER_CONTEXT.setBaseImageURL(<nowiki>"http:// localhost/myimages"</nowiki>);
        // Tell the Engine where to write the images to
        RENDER_CONTEXT.setImageDirectory("C:/xampplite/htdocs/myimages");
        // Tell the Engine what image formats are supported.  Note you must have SVG in the string 
        // to render charts in SVG.
        RENDER_CONTEXT.setSupportedImageFormats("JPG;PNG;BMP;SVG");
        final HashMap<String, HTMLRenderContext> CONTEXT =
              new HashMap<String, HTMLRenderContext>();
        CONTEXT.put(EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT, RENDER_CONTEXT);
        TASK.setAppContext(CONTEXT);
        // Set PARAMETERS for the report
        TASK.setParameterValues(PARAMETERS);
        // Alternatively set each separately
        // TASK.setParameterValue("Top Count", new Integer(12));
        TASK.validateParameters();
 
        // Add a scriptable object, which will allow the report developer to put
        // script in the report that references this Java object, e.g. in script 
        // pFilter.myjavamethod()
        // final ProcessFilter PF = new ProcessFilter();
        // TASK.addScriptableJavaObject("pFilter", PF);
 
        // Set rendering options - such as file or stream output, 
        // output format, whether it is embeddable, etc
        final HTMLRenderOption HTML_OPTIONS = new HTMLRenderOption();
 
        // Remove HTML and Body tags
        // HTML_OPTIONS.setEmbeddable(true);
 
        // Set output location
        HTML_OPTIONS.setOutputFileName("C:/test/2.1/output.html");
 
        // Set output format
        HTML_OPTIONS.setOutputFormat("html");
        TASK.setRenderOption(HTML_OPTIONS);
 
        // run the report and destroy the engine
        // Note - If the program stays resident do not shutdown the Platform or the Engine
        TASK.run();
        TASK.close();
        engine.shutdown();
        Platform.shutdown();
        System.out.println("Finished");
    }
 
    /**
     * @param ARGUMENTS
     */
    public static void main(final String[] ARGUMENTS) {
        try {
           executeReport();
        } catch (final Exception EX) {
           EX.printStackTrace();
        }
    }
}

BIRT 2.2

public void executeReport() throws EngineException {
 
    IReportEngine engine = null;
    EngineConfig config = null;
 
    try {
        config = new EngineConfig();          
        config.setBIRTHome("C:\\birt\\birt-runtime-2_2_1\\birt-runtime-2_2_1\\ReportEngine");
        config.setLogConfig("c:/temp/test", Level.FINEST);
        Platform.startup(config);
        final IReportEngineFactory FACTORY = (IReportEngineFactory) Platform
            .createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
        engine = FACTORY.createReportEngine(config);       
 
        // Open the report design
        IReportRunnable design = null;
        design = engine.openReportDesign("Reports/customers.rptdesign"); 
        IRunAndRenderTask task = engine.createRunAndRenderTask(design);        
        // task.setParameterValue("Top Count", (new Integer(5)));
        // task.validateParameters();
 
        final HTMLRenderOption HTML_OPTIONS = new HTMLRenderOption();       
        HTML_OPTIONS.setOutputFileName("output/resample/Parmdisp.html");
        HTML_OPTIONS.setOutputFormat("html");
        // HTML_OPTIONS.setHtmlRtLFlag(false);
        // HTML_OPTIONS.setEmbeddable(false);
        // HTML_OPTIONS.setImageDirectory("C:\\test\\images");
 
        // PDFRenderOption PDF_OPTIONS = new PDFRenderOption();
        // PDF_OPTIONS.setOutputFileName("c:/temp/test.pdf");
        // PDF_OPTIONS.setOutputFormat("pdf");
 
        task.setRenderOption(HTML_OPTIONS);
        task.run();
        task.close();
        engine.destroy();
    } catch(final Exception EX) {
        EX.printStackTrace();
    } finally {
       Platform.shutdown();
    }
}

In order to compile and run the sample code, you have to make sure that all the necessary libraries are in the classpath. I assume you are using eclipse as your development environment and that you created a java project where you created a java file containing the sample code above. Also you should have downloaded and installed the birt-runtime. (I guess you have because otherwise what did you set the engine home in the code above?)

First of all, the question is which libraries do you have to add? The answer is quite easy: All the libraries in the birt-runtime-2_1_1/ReportEngine/lib directory. So all you have to do is to right click the project and choose the properties action to open the project property dialog. There select the "Java Build Path" prperty page. Go to the Libraries tab and add the above mentioned libraries using the "Add external jars ..." button.

Comments

Please enter comments below by selecting the edit icon to the right. You will need a Bugzilla account to add comments.


Thank you so much for nice example. But mine is getting below error. Please advise.

Error : A JNI error has occurred , please check your installation and try again

Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes

I downloaded and copied birtruntime as same path.

C:/birt-runtime-4_4_2/ReportEngine

Back to the top