Failed to access class org.bson.BSON from class com.mongodb.DBObjectCodec

I’m trying to make an API using RESTeasy and jetty, and I’m trying to use Morphia to do my database operations. However I’m running into the following error message:

java.lang.IllegalAccessError: failed to access class org.bson.BSON from class com.mongodb.DBObjectCodec (org.bson.BSON and com.mongodb.DBObjectCodec are in unnamed module of loader org.eclipse.jetty.webapp.WebAppClassLoader @7434ee13)

I’m using Java 11 and for Morphia I’m using 1.3.2 (I know this is an older version but the newer versions seem to not even get to this point and crash upon creating a datastore.

The code that is generating this error is as follows:

final String database = "<database name>";
        
final MongoClientURI databaseURI = new MongoClientURI("<connection string>");

Morphia morphia = new Morphia();

final Datastore datastore = morphia.createDatastore(new MongoClient(databaseURI), database);

Model model = new Model();

model.setCostumer("TestCostumer");

datastore.save(model);

If further info is required I’ll do my best to provide it.

My first guess is that your application is pulling in an incompatible version of the MongoDB Java driver (on which Morphia depends). Check your dependencies to rule that out. (From morphia/gradle.properties at r1.3.2 · MorphiaOrg/morphia · GitHub it looks like you need the 3.4 driver)

Good luck

Many thanks for your reply.

Whilst the version you named was not the correct version you did put me on the right track.
I ended up using the following MondoDB and Morphia dependencies in my pom

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>3.12.14</version>
</dependency>
<dependency>
    <groupId>org.mongodb.morphia</groupId>
    <artifactId>morphia</artifactId>
    <version>1.3.2</version>
</dependency>

I’m listing these in case someone else might run into the same issue I ran into.

I’d love to see the stacktrace from that crash. That’s a fundamental method in Morphia so crashing should “never” happen. I have a feeling it’s likely a version mismatch somewhere but if you’re up for it, I’d love to try and diagnose that with you so you can move to a newer Morphia. 1.3.2 isn’t just old. It’s practically paleolithic. :slight_smile:

I’ll try looking up the stacktrace later. I’m currently busy.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.