Altova MapForce 2025 Professional 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\MapForce2025\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:

 

AltovaAutomation.dll

Java-COM bridge: DLL part

AltovaAutomation.jar

Java-COM bridge: Java library part

MapForceAPI.jar

Java classes of the MapForce API

RunMapForce.java

Java example source code

BuildAndRun.bat

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

.classpath

Eclipse project helper file

.project        

Eclipse project file

MapForceAPI_JavaDoc.zip

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 RunMapForce.java 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 RunMapForce.java 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.
    try
    {
        // Start MapForce as COM server.
        mapforce = new Application();
        // COM servers start up invisible so we make it visible
        mapforce.setVisible(true);
       
        // 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()
        {
          @Override
          public void onDocumentOpened(Document i_ipDoc) throws AutomationException
          {
              String name = i_ipDoc.getName();
             
              if (name.length() > 0)
                System.out.println("Document " + name + " was opened.");
              else
                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);
        display(err1);
        // open CompletePO.mfd and generate c++ code
        Document doc = mapforce.openDocument(strExamplesFolder + "CompletePO.mfd");
        ErrorMarkers err2 = doc.generateCodeEx(ENUMProgrammingLanguage.eCpp);
        display(err2);
       
        doc.close();
        doc = null;
           
        System.out.println("Watch MapForce!");
    }
    catch (AutomationException e)
    {
        // e.printStackTrace();
    }
    finally
    {
        // Make sure that MapForce can shut down properly.
        if (mapforce != null)
          mapforce.dispose();
 
        // 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.\n");
     
    while (itr.hasNext())
    {
        String sError = "";
        Object element = itr.next();
        if (element instanceof ErrorMarker)
            sError = ((ErrorMarker)element).getText();
        System.out.print("Error text: " + sError + "\n");
    }
  }
}

© 2018-2024 Altova GmbH