Launching AnyLogic Model from External Application

AnyLogic provides users with the easy mechanism of integrating AnyLogic models with external Java applications. 

AnyLogic comes with a set of special How To models demonstrating different integration scenarios and containing code that can be used as a starting point in case you need some particular integration case and want to develop the given solution further in your Java project.

This article describes how to launch AnyLogic model (and display its model window enabling the user to control the model execution) from external Java application. This integration case is implemented in the following how-to model: 

How-To model: Launching AnyLogic Model from External Application

Let's study what should be done to launch the model from external Java application so that its model window is shown. Please open this how-to model by clicking the link given above. Study the model inner structure. 

The model contains:

Please open MyApplication Java class by double-clicking it in the Projects view. You will see the following code there:


System.out.println() calls just output some information about the model run progress, so you should pay your great attention here to just three lines.

First of all, to the following  line: 

final Simulation s = new Simulation();

Here the constructor of the simulation experiment named Simulation is called. Please note that Simulation here is the name of the simulation experiment in this particular model, not the name of the base class for simulation experiments in AnyLogic. In case the experiment is named MyExperiment, you need to write MyExperiment s = new MyExperiment();

Then you may notice the following function being called:

s.setup(null);

The function setup() of the experiment performs the initial setup of the simulation engine and the presentation, if any. It creates the model runtime window and returns the execution to the main thread. The only argument of the function takes is the external container - if it is embedded in some container, or null (as in our case) if it is run in standalone application mode

Finally the experiment function runTheModel() is called. This function is declared right in the model, you can find it on the simulation experiment's diagram. It does the same things as usual "Run the model and switch to Main view" button on the simulation experiment page does - runs the model and shows the presentation of the model's top level agent in the model window. Calling this function we just prevent users from clicking this button on model start - everything is done automatically now.

Also you can see that you can easily set up experiment parameter values - they are accessible as experiment fields simply by their names. Here we set 0.012345 as the value of Fraction parameter by writing s.Fraction=0.012345;  Note that your external application may choose and set up arbitrary values for the model parameters.

 To check how this integration scenario works

  1. Open this how-to model and export it as standalone application.
  2. Modify bat/sh/cmd file by changing class name from Simulation to MyApplication.
  3. Run the bat/sh/cmd file and see how it works.

If you want to use the described technique in your application and continue developing it further in some Java IDE, please follow the steps described below. We will describe how to apply this solution  in Java project created in Java IDE Eclipse. If you do not have Java project in Eclipse, create it as described here (online help documentation on Eclipse web site). 

 To use the described technique in your Java application

  1. First of all we need to export the model as standalone application (we recommend to export the model directly to the folder of your Java project).
  2. Add the .jar files created on the model export to your Java project. Therefore first refresh the project tree by right-clicking (Mac OS: Ctrl+clicking) the project item in the tree and choosing the Refresh command from the pop-up menu.

  1. Now you will see new files appeared in the project folder.



    Select them and choose Build path > Add to build path from the context menu. You need to add compiled .jar file of the model and also .jar files required for successful model compilation. But do not add the bat/cmd/sh file since it is purposed just for launching the exported model and we do not need it in our project.


  2. Import the package containing the model classes by writing the following line in the very beginning of the class code in your project:

    import <model_package_name>.*;

    (You can know the model package name in AnyLogic, in the Advanced properties section of the model)



    So, in our particular case we will write

    import launching_model.*;



    Please note that you can use usual Code Completion (displayed on pressing Ctrl+space) while typing code in Eclipse.

  3. Now you can refer to model classes in the code of your project. The easiest way to implement the described solution is just to copy the function main() from the model's Java class MyApplication into the class of your Java project:

That's all, now you can run the class as Java application. You will see that the launched application opens the model window. It is the usual model window, that is created and displayed on the model run.

You can see the output information displayed in the Eclipse Console view.


Related topics

Integrating AnyLogic models with external Java applications

Running the model from outside without model window