Java APIs for Distributed Mode

In your Java source code, include the necessary code to run your report application.

To use the Genero Report Writer with your Java applications, add either gre.jar or greclient.jar to the CLASSPATH. These files are located in the gre/lib/jars/ directory.

Provide the remote connection details

In the .java source code, include the following:
RemoteConnection remoteConnection = new RemoteConnection(report);
remoteConnection.setHost("GRE_server_name");
remoteConnection.setPort("port");
  • GRE_server_name is the name of the server where the GRE is running.
  • port is the port number where the GRE is listening.

Simple code sample

The code sample below updates the simple Java example to include the remote connection details for a GRE on a server named "huascar", listening on port 1974.
/*
 * FOURJS_START_COPYRIGHT(U,2003)
 * Property of Four Js*
 * (c) Copyright Four Js 2003, 2022. All Rights Reserved.
 * * Trademark of Four Js Development Tools Europe Ltd
 *   in the United States and elsewhere
 * 
 * Four Js and its suppliers do not warrant or guarantee that these samples are
 * accurate and suitable for your purposes.
 * Their inclusion is purely for information purposes only.
 * FOURJS_END_COPYRIGHT
 */

import com.fourjs.report.runtime.*;
import java.awt.Desktop;
import java.io.File;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.IOException;
import javax.xml.bind.JAXBException;
import org.xml.sax.SAXException;
import java.util.Vector;
import java.util.Date;
import java.io.Serializable;

//The XmlRootElement annotation causes an global element declaration to be produced in the schema.
@XmlRootElement
public class Sales {
//The XmlElement annotation maps a property to an XML element.
//The Genero Engine does not support optional variables so setting "required=true" is mandatory.
//If a variable needs to be optional then use "nillable=true" and set the variable to null when 
//there is no value. If a primitive type needs to be optional then use the corresponding 
//object wrapper instead (e.g java.lang.Integer for "int").
   @XmlElement(required = true, nillable = true)
   public String shopName;
   @XmlElement(required = true)
   public int zipCode;
   @XmlElement(required = true, nillable = true)
   public Date day;
 

//This and any other public collection will be serialized in the order they are declared descending //recursively into the classes contained in the collections.
    public Vector<SalesItem> items=new Vector<SalesItem>();

    public Sales(String shopName, int zipCode, Date day)
    {
        this.shopName=shopName;
        this.zipCode=zipCode;
        this.day=day;
        items.add(new SalesItem("Tablelamp",SalesItem.Category.Furniture,23.00,null));
        items.add(new SalesItem("Tablelamp",SalesItem.Category.Furniture,267.00, items.lastElement()));
        items.add(new SalesItem("Officechair",SalesItem.Category.Furniture,155.00, items.lastElement()));
        items.add(new SalesItem("Grandfather clock",SalesItem.Category.Furniture,329.00, items.lastElement()));
        items.add(new SalesItem("Scissors",SalesItem.Category.Supplies,19.00, items.lastElement()));
        items.add(new SalesItem("Measuring tape",SalesItem.Category.Supplies,23.00, items.lastElement()));
        items.add(new SalesItem("Sunglasses",SalesItem.Category.Travelling,15.95, items.lastElement()));
        items.add(new SalesItem("Penknife",SalesItem.Category.Travelling,6.25, items.lastElement()));
        items.add(new SalesItem("Ornateangel",SalesItem.Category.Art,1.95, items.lastElement()));
    }

/** Default Constructor that is required for JAXB deserialization.
    In this program this is never used.
*/
    public Sales() { assert false; }

/**
    Runs the report using the design file specified in args[0] or "SalesList.4rp" otherwise.
    The program creates the file "SalesList.pdf" and opens it using
Desktop.open() which will typically
    invoke the Acrobat Reader.
*/
    public static void main(String[] args) throws JAXBException, IOException, SAXException
    {
        String designFile;
        String outputFilename = "SalesList.pdf";
    
        if (args.length == 0) {
            designFile = "SalesList.4rp";
        } else {
            designFile = args[0];
        }

        FormatHandler handler = new FormatWriter(outputFilename);
        PDFRenderer renderer = new PDFRenderer(handler);
        FourRpLayouter report = new FourRpLayouter(designFile, renderer);

        //Set Distributed Mode
        RemoteConnection remoteConnection = new RemoteConnection(report);
        remoteConnection.setHost("huascar");
        remoteConnection.setPort(1974);
        //

        report.setDebugLevel(9);
        Sales data = new Sales("Columbus Arts", 75038, new Date());

        remoteConnection.runFromJAXBObject(data);
    
        // open the file
        File result = new File(outputFilename);
        Desktop desktop = Desktop.getDesktop();
        desktop.open(result);

  }

}

class SalesItem
{
    public enum Category {Furniture, Art, Supplies, Travelling };
    
    @XmlElement(required = true, nillable = true)
    public String articleName;
    @XmlElement(required = true, nillable = true)
    public Category category;
    @XmlElement(required = true)
    public double price;
    @XmlElement(required = true)
    public double runningTotal;
    
//The previous item is passed to allow computing the running total.
    public SalesItem(String articleName, Category category, double price, SalesItem previousItem)
    {
        this.articleName=articleName;
        this.category=category;
        this.price=price;
       
this.runningTotal=previousItem==null?price:previousItem.runningTotal+price;
    }

/** Default Constructor that is required for JAXB deserialization.
    In this program this is never used.
*/
    public SalesItem() { }
}