AnyLogic provides you with the unique ability to use any external Java classes in your model.
To do this, you should:
- Import archive files (*.jar, *.zip) with Java classes, or external class folder to your AnyLogic model.
- Add import statements in the model, or refer instances of external Java classes by full classpath
Users typically refer to external class folders when the model development is in progress and Java classes are frequently modified. After you finish the development, we recommend you to archive your Java classes in a JAR file and link it to the model instead of external class folder.
Java archive files and class folders needed by a model should be added to the model dependencies list. You can manage it in the Dependencies section of the model's properties view. When exporting your model, all resources defined in this list will be copied in the destination folder of the exported model.
To add external Java classes (as Java archive file, or external class folder) to a model dependencies list
Select the model in the Projects view (the top-level item in the project tree).
Go to the Dependencies section of the Properties view. You manage the list of external JAR files and Java classes used by the model in the Jar files and class folders required to build the model table (the second one in the Dependencies section).
In the model dependencies section, click Add button to the right of the Jar files and class folders required to build the model table. This opens the Add Classpath Entry dialog box.
- Choose the Type of the added resource: Java Archive File(*.jar, *.zip), or External Class Folder.
- If you are plugging in the Java archive file, you can import Java file(s) to the model folder by selecting the Import to model folder option. Importing Java archive file to the model folder, you make this model easily portable (when delivering the model to some customer, you have just to copy the whole model folder). Alternatively, you can choose the option Access file(s) from original location. In this case the JAR file will not be copied to the model folder, and the model will access the file from its current location. It can be helpful when you have several models accessing the same JAR file. If for example, you will need to update the JAR file, you will be able to update it just in one place (in its current location), and all the models will automatically refer to the updated version without any changes.
- Specify the path to the file/folder in the File / Folder field. You can refer to the files using either absolute, or relative path. Select the corresponding option below (Use absolute path, or Use relative path), and then specify the path in the File / Folder field, or browse to it using the standard file chooser window that opens on clicking the Browse button.
- Click Finish. Upon completions of this operation, the source file will automatically appear in the model's Resource folder in the Projects view. This way you will be able to track the current state of the source file, switch between the absolute and relative file paths, etc
After setting the dependencies, you can use external Java classes in your model.
The following demo model demonstrates how you can refer the external Java classes within the model. In this model we use external Java library, JAMA. JAMA is a basic linear algebra package for Java. In our model we have three variables (A, B, C) — instances of external Jama.Matrix class, designed as the standard matrix class for Java.
Demo model: External JAR files
There are two alternative ways to refer to external Java classes from your AnyLogic model.
Declare and refer to the instances of external Java classes using full class names (prefixed with the package name), as we do with A
variable in the demo model. You can see that we use the full class name as the Type of the variable (Jama.Matrix), and we also use the full class name when we initialize this variable with the Java constructor, see the variable's Initial value property:
new Jama.Matrix( 2, 5 )
We recommend to follow this way only when you have the limited number of references to external Java classes in your model.
- Import the classes in the model by adding the import statement(s) in the model's agent properties. Having done this, you can refer the classes using the short class names, without package name prefixes. To add import statement, open the properties of the agent type that references to the external Java classes (in this model it isMain).
Open the Advanced Java section of its properties, and type
to import some specific class of the plugged-in Java library. Example:
Even easier is to import the whole package once by typing there
Example: import Jama.*;
Having imported the Java package, you can refer to its classes easier (as we do with the variable B in the demo model), it is just of Type Matrix.
We recommend to follow this particular way: import the whole package and simplify access to the external Java classes within your model.
If some resource is not used by a model anymore, it can be removed from the dependencies of this model.
To remove an external JAR file or a class folder from a model dependencies list
- Select the model in the Projects view (the top-level item in the project tree).
- Go to the Dependencies section of the Properties view.
- In the Jar files and class folders required to build the model table (the second one in the Dependencies section), select the resource you do not need anymore, and want to remove from the dependencies list.
Click Remove button to the right of the Jar files and class folders required to build the model table.
How can we improve this article?