Altova MapForce 2023 Enterprise Edition

Example Java Project

Home Prev Top Next

After you install MapForce, an example MapForce API client project for Java is available in the directory C:\Users\<username>\Documents\Altova\MapForce2023\MapForceExamples\API.


You can test the Java example directly from the command line, using the batch file BuildAndRun.bat, or you can compile and run the example project from within Eclipse. See below for instructions on how to use these procedures.


File list

The Java examples folder contains all the files required to run the example project. These files are listed below:



Java-COM bridge: DLL part


Java-COM bridge: Java library part


Java classes of the MapForce API

Java example source code


Batch file to compile and run example code from the command line prompt. Expects folder where Java Virtual Machine resides as parameter.


Eclipse project helper file


Eclipse project file

Javadoc file containing help documentation for the Java API


What the example does

The example starts up MapForce and performs a few operations, including opening and closing documents. When done, MapForce stays open. You must close it manually.


Running the example from the command line

To run the example from the command line, open a command prompt window, go to the Java folder of the API Examples folder (see above for location), and then type:


 buildAndRun.bat "<Path-to-the-Java-bin-folder>"


The Java binary folder must be that of a Java Development Kit (JDK) 7 or later installation on your computer.


Press the Return key. The Java source in will be compiled and then executed.


Loading the example in Eclipse

Open Eclipse and use the File | Import... | General | Existing Projects into Workspace command to add the Eclipse project file (.project) located in the Java folder of the API Examples folder (see above for location). The project RunMapForce will then appear in your Package Explorer or Navigator.


Select the project and then the command Run as | Java Application to execute the example.


Note:You can select a class name or method of the Java API and press F1 to get help for that class or method.


Java source code listing

The Java source code in the example file is listed below with comments.


// access general JAVA-COM bridge classes
import java.util.Iterator;
import com.altova.automation.libs.*;
// access XMLSpy Java-COM bridge
import com.altova.automation.MapForce.*;
import com.altova.automation.MapForce.Enums.ENUMProgrammingLanguage;
* A simple example that starts the COM server and performs a few operations on it.
* Feel free to extend.
public class RunMapForce
  public static void main(String[] args)
    // an instance of the application.
    Application mapforce = null;
    // instead of COM error handling use Java exception mechanism.
        // Start MapForce as COM server.
        mapforce = new Application();
        // COM servers start up invisible so we make it visible
        // The following lines attach to the application events using a default implementation
        // for the events and override one of its methods.
        // If you want to override all document events it is better to derive your listener class
        // from DocumentEvents and implement all methods of this interface.
        mapforce.addListener(new ApplicationEventsDefaultHandler()
          public void onDocumentOpened(Document i_ipDoc) throws AutomationException
              String name = i_ipDoc.getName();
              if (name.length() > 0)
                System.out.println("Document " + name + " was opened.");
                System.out.println("A new mapping was created.");
        // Locate samples installed with the product.
        int majorVersionYear = mapforce.getMajorVersion() + 1998;
        String strExamplesFolder = System.getenv("USERPROFILE") + "\\Documents\\Altova\\MapForce" + Integer.toString(majorVersionYear) + "\\MapForceExamples\\";
        // create a new MapForce mapping and generate c++ code
        Document newDoc = mapforce.newMapping();
        ErrorMarkers err1 = newDoc.generateCodeEx(ENUMProgrammingLanguage.eCpp);
        // open CompletePO.mfd and generate c++ code
        Document doc = mapforce.openDocument(strExamplesFolder + "CompletePO.mfd");
        ErrorMarkers err2 = doc.generateCodeEx(ENUMProgrammingLanguage.eCpp);
        doc = null;
        System.out.println("Watch MapForce!");
    catch (AutomationException e)
        // e.printStackTrace();
        // Make sure that MapForce can shut down properly.
        if (mapforce != null)
        // Since the COM server was made visible and still is visible, it will keep running
        // and needs to be closed manually.
        System.out.println("Now close MapForce!");
  public static void display(ErrorMarkers err) throws AutomationException
    Iterator<ErrorMarker> itr = err.iterator();
    if (err.getCount() == 0)
        System.out.print("Code generation completed successfully.\");
    while (itr.hasNext())
        String sError = "";
        Object element =;
        if (element instanceof ErrorMarker)
            sError = ((ErrorMarker)element).getText();
        System.out.print("Error text: " + sError + "\");

© 2016-2022 Altova GmbH