The following example illustrates how to run a mapping execution file (.mfx) from Java code. On Windows, all the example files are available at the following path: C:\Program Files\Altova\MapForceServer2022\etc\Examples. They are as follows:
•TokenizeString.mfx - A MapForce Server execution file that will be run by the Java program.
•AltovaTools.xml - An XML file that provides input data to the mapping program.
On Linux and Mac platforms, no examples are pre-installed; however, you can prepare an executable .mfx file like the one run in this example as follows:
1.Open the desired mapping design file (.mfd) with MapForce on Windows.
2.On the File menu, select Mapping Settings, and clear the Make paths absolute in generated code check box if it is selected.
3.For each mapping component, open the Properties dialog box (by double-clicking the component's title bar, for example), and change all file paths from absolute to relative. Also, select the Save all file paths relative to MFD file check box. For convenience, you can copy all input files and schemas in the same folder as the mapping itself, and reference them just by the file name. Refer to MapForce documentation for more information about dealing with relative and absolute paths while designing mappings.
4.On the File menu, select Compile to MapForce Server Execution file. This generates the .mfx file that you will subsequently run with MapForce Server, as shown in the code listing below.
Before running the code below, ensure the following prerequisites are met:
•MapForce Server is installed and licensed
•The Java CLASSPATH includes a reference to the MapForceServer.jar library (for an example, see About the Java Interface).
If you are using a custom .mfx file as shown above, there may be other prerequisites, depending on the kind of data processed by the mapping. For more information, see Preparing Mappings for Server Execution.
On Windows, the example Java project is in the "Program Files" directory, which requires administrative rights. You will either need to run your Java development environment (for example, Eclipse) as administrator, or copy the example to a different folder where you don't need administrative rights.
The code listing below first creates a MapForce Server object. Next, it sets the working directory where the application should look for any files that act as input to the mapping, and where it should generate the mapping output files. As mentioned above, the example mapping file reads data from a source XML file—so make sure that both the XML file and its schema exist in the working directory.
The setServerPath method specifies the path to the MapForce Server executable. For example, on Ubuntu, this would be /opt/Altova/MapForceServer2022/bin/mapforceserver. You can omit the setServerPath if you did not move the MapForceServer.jar from its default location.
Finally, the run method runs a mapping (.mfx file) that was compiled with MapForce (in this example, TokenizeString.mfx). On success, the program below generates a CSV file in the working directory. On error, the program attempts to print out the last execution message generated by MapForce Server.
public class Program