Adding External Java Classes

AnyLogic provides you with the unique ability to use any external Java classes in your model. Just one example: you can add 3D charts in your model by plugging in the external Java library. 

To do this, you should: 

  1. Import archive files (*.jar, *.zip) with Java classes, or external class folder to your AnyLogic model. 
  2. 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

  1. Select the model in the Projects view (the top-level item in the project tree).
  1. 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 secion).
  1. 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.
  1. Choose the Type of the added resource: Java Archive File(*.jar, *.zip), or External Class Folder.
  2. 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.
  3. 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.
  4. Click Finish.

Referring the external Java classes within AnyLogic model

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.

Removing the Java classes from the model dependencies

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
  1. Select the model in the Projects view (the top-level item in the project tree).
  1. Go to the Dependencies section of the Properties view. 
  2. In the Jar files and class folders required to build the model table (the second one in the Dependencies secion), select the resource you do not need anymore, and want to remove from the dependencies list.
  3. Click Remove  button to the right of the Jar files and class folders required to build the model table.