3

I have an AnyLogic model that uses JAXB functions for parsing an XML file. The model used to work earlier but it doesn't now since apparently the newer versions of Java don't include JAXB. The online examples of how to do this in Java programs doesn't fit AnyLogic environment.

Based on online searches, I have downloaded and included the jaxb-api-2.4.0-b180830.0359.jar file in AnyLogic model. That by itself doesn't work and leads to the following error:

SEVERE: null javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.

I then added the following in the import section:

 import java.xml.bind;
 import com.sun.xml.bind;

Also tried:

 import java.xml.bind.*;
 import com.sun.xml.bind.*;

Both resulted in the same error:

The import com.sun.xml.bind cannot be resolved. The import java.xml cannot be resolved.

On line guidance from for example https://www.dariawan.com/tutorials/java/using-jaxb-java-11/ recommend adding the dependencies using below code to Java programs:

    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>${jaxb.api.version}</version>
    </dependency>

How do we specify such dependencies via AnyLogic interface?

Once JAXB functions work, they should lead to parsing the data in XML file and creating and populating corresponding objects in AnyLogic as was the case before.

2 Answers2

0

Sounds like you didn't add the .jar file properly in AnyLogic...

Click on your model in the projects view (the top-most entry above all agents and Main): enter image description here

In the properties, you can add your .jar file under the "Dependencies" tab: enter image description here

Do follow the advise to "keep a copy in your model folder" so it doesn't get lost.

Now you can import what you need and it will work (unless the .jar file is broken itself).

PS: Might be better to rephrase your question to "How to load an external jar file to my model dependencies"

Benjamin
  • 10,603
  • 3
  • 16
  • 28
  • 1
    `@Benjamin` Thanks for your input. Our current model loads a number of other jar files successfully, some of which I have added myself following the same method that you suggested. And the jaxb-api-2.4.0-b180830.0359.jar file does show up in the list of Jar files. But I get the error that is in my question above. In my understanding there is something more to importing JAXB jar file. Other Java programmers have reported problems and they were not even using AnyLogic -- please search with error message on StackOverflow & GitHub to see those (I hit the char limit else would add links). – Sanjay Jain Aug 16 '19 at 02:07
  • Well, then it is a problem with that jar file, no? – Benjamin Aug 16 '19 at 05:02
0

The problem

As already stated in the question, the problem is this: JAXB as a Java tool for XML treatment has been removed from the Java standard library beginnig with Java 9. The reason for this decision was to make the standard Java library more lightweight.

Dependencies in Java

This can be solved by including the removed package(s) manually into your project. As a lot of people already had this problem, quite an amount of SO questions for this exist, like this and this. In these answers it is suggested to 'load dependecies', eg like this:

<!-- https://mvnrepository.com/artifact/pacakgename -->
<dependency>
    <groupId>packagename</groupId>
    <artifactId>modulename</artifactId>
    <version>1.0.0</version>
</dependency>

These dependency statements are interpreted by the Java IDE such as Eclipse (with a package manager such as Maven), and the stated packages then get automatically loaded from an online package repository and included in the project.

In AnyLogic, the procedure is slightly different!

Dependencies in AnyLogic

In AnyLogic the exact same thing is happening when you click on your project in the AnyLogic editor, and add a JAR file under the Dependencies tab, see Benjamin's answer for this. In order to do that you will manually have to find the JAR file in a package repository and download it first.

Needed Pacakges

This is where I am not completely sure. I'll got an example to run when I included the following packages, but probably there is redundancy, so you might want to try around with variations of them:

javax.xml.bind / jaxb-api / 2.3.0-b170201.1204

javax.activation / activation / 1.1

org.glassfish.jaxb / jaxb-runtime / 2.3.0-b170127.1453

com.sun.xml.bind / jaxb-impl / 2.2.11

com.sun.xml.bind / jaxb-core / 2.2.11

Example

I created a simple example model in AnyLogic, that is based on this blog post. You can run and download it (including the dependeny Java packages) here.

Florian
  • 962
  • 1
  • 5
  • 17