Java API Example
The UModel installation package contains an example Java project, located at C:\Users\<username>\Documents\Altova\UModel2025\UModelExamples\API. This folder contains Java examples for the UModel API. You can test it 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.
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 |
UModelAPI.jar | Java classes of the UModel API |
RunUModel.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 |
UModelAPI_JavaDoc.zip | Javadoc file containing help documentation for the Java API |
Readme.txt | This file |
The example starts up UModel and performs a few operations, including opening and closing documents. When done, UModel 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 JDK 1.7 or later installation on your computer.
Press the Return key. The Java source in RunUModel.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 RunUModel 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. |
Potential issues with instanceof and cast operators
There could be some issues with the instanceof and cast operators. For example, the instanceof and cast operators do not work when you receive instances of a base class type (e.g., UMLElement.getOwnedElements()). Note the following recommendations:
•You should not use the instanceof operator and class casts when you receive instances of base classes directly from the API.
•Use a member function instead to determine the type of the element. Then create a new instance of the derived class.
An extract of the code listing from RunUModel.java will give you an idea about how to work around potential issues with the instanceof and cast operators:
private static void printUMLTree(UMLData i_data, String tab) throws AutomationException { // Java's 'instanceof' operator does not work where we receive instances of a base class type like with 'UMLElement.getOwnedElements()'. if (i_data.isKindOf("Package")) { // the Java cast operator does not work for these objects either. Instead, create a new instance with the appropriate class type. UMLPackage umlPackage = new UMLPackage(i_data); // (UMLPackage)i_data
if (umlPackage.getIsShared()) System.out.println(tab + "Shared Package " + umlPackage.getName()); else System.out.println(tab + "Package " + umlPackage.getName()); } else if (i_data.isKindOf("Class")) { System.out.println(tab + "Class " + new UMLClass(i_data).getName()); }
// recurse tab += " "; if (i_data.isKindOf("Element")) for (UMLData elem : new UMLElement(i_data).getOwnedElements()) printUMLTree(elem, tab); } |