[JAVA-4947] bson-record-codec jar requires Java 17 and is non-optional dependency of mongodb-driver-core jar Created: 19/Apr/23  Updated: 27/Oct/23  Resolved: 19/Apr/23

Status: Closed
Project: Java Driver
Component/s: Packaging
Affects Version/s: None
Fix Version/s: None

Type: Question Priority: Major - P3
Reporter: Ian Springer Assignee: Unassigned
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Summary

Please provide a clear and concise description of the bug.

Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

How to Reproduce

Steps to reproduce. If possible, please include a Short, Self Contained, Correct (Compilable), Example.

Additional Background

Please provide any additional background information that may be helpful in diagnosing the bug.



 Comments   
Comment by Jeffrey Yemin [ 19/Apr/23 ]

Glad to hear you discovered the workaround, and posted it as well.

Under normal circumstances the bson-record-codec dependency (which is the one causing this issue) will never be loaded by an application unless it is actually using Java records (which already implies Java 17), so it doesn’t cause problems for the great majority of applications. But some tools (like shaders) more aggressively traverse transitive dependencies, and bson-record-codec can cause issues.

For Gradle users who might encounter this, you can exclude the bson-record-codec dependency from your build using Gradle dependency management features. See here for details.
 

Comment by Ian Springer [ 19/Apr/23 ]

See https://www.mongodb.com/community/forums/t/version-4-6-1-and-4-7-1-not-compatible-with-java-11/179107/1 for someone else who encountered this issue with a Gradle project.

Comment by Ian Springer [ 19/Apr/23 ]

My project is a multi-module Maven project that targets Java 11.

Upon attempting to upgrade the Mongo Java driver dependencies in my project to v4.6 or later, the build failed with the following error when attempting to create shaded jars for several modules:

 

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:3.2.4:shade (build-workbench-shaded-jar) on project workbench: Error creating shaded jar: Problem shading JAR /Users/ian.springer/.m2/repository/org/mongodb/bson-record-codec/4.9.1/bson-record-codec-4.9.1.jar entry org/bson/codecs/record/RecordCodec$ComponentModel.class: java.lang.IllegalArgumentException: Unsupported class file major version 61 -> [Help 1]

 

Using "mvn dependency:tree", I found that bson-record-codec is a dependency of mongodb-driver-core. Since the Mongo Java driver is supposed to be compatible with Java 8 and later, the bson-record-codec should be an optional dependency, so confusing build errors like this are not encountered. Then users who are using Java 17 or later will simply have add an explicit bson-record-codec dependency if they need to serialize Records to Mongo.  

 

Note, I was able to work around the errors by excluding bson-record-codec in my root POM as follows:

 

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongodb-driver-core</artifactId>
            <version>${mongodb.driver.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.mongodb</groupId>
                    <artifactId>bson-record-codec</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
</dependencyManagement>

 

Generated at Thu Feb 08 09:03:21 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.