You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GlassFish集成MongoDB时出现NoClassDefFoundError问题求助

Fixing java.lang.NoClassDefFoundError: com/mongodb/client/MongoCollection When Running GlassFish with MongoDB

When running a GlassFish server with MongoDB, I encountered the following error: java.lang.NoClassDefFoundError: com/mongodb/client/MongoCollection. My dependency configuration is as follows:

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.containers/jersey-container-servlet -->
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.26</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-json-jackson -->
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.26</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-client -->
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>1.19.4</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.mongodb/mongodb-driver -->
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver</artifactId>
        <version>3.6.1</version>
    </dependency>
</dependencies>

I've confirmed the relevant JARs are imported, there are no compilation errors, and I've looked up related solutions without success. Please help me troubleshoot this issue, thank you!

Let's work through this runtime classloading issue step by step—since your code compiles fine, the problem is almost certainly how the server is accessing the MongoDB driver classes.

Possible Solutions

1. Fix Jersey Version Conflicts (Critical!)

You're mixing Jersey 2.x (your glassfish.jersey dependencies) and Jersey 1.x (com.sun.jersey:jersey-client). These two versions are completely incompatible and can wreak havoc with classloading, which often causes indirect errors like missing MongoDB classes. Replace your Jersey client dependency with the 2.x version that matches your other Jersey artifacts:

<!-- Remove the com.sun.jersey:jersey-client dependency and add this -->
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>2.26</version>
</dependency>

2. Verify Full MongoDB Driver Dependency Resolution

The mongodb-driver 3.6.1 artifact relies on two transitive dependencies: mongodb-driver-core and bson. Sometimes Maven fails to pull these in correctly due to mirror issues or dependency overrides. Run this command to check your dependency tree:

mvn dependency:tree

Make sure you see all three artifacts with matching 3.6.1 versions. If any are missing, add them explicitly to your pom.xml:

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongodb-driver-core</artifactId>
    <version>3.6.1</version>
</dependency>
<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>bson</artifactId>
    <version>3.6.1</version>
</dependency>

3. Deploy MongoDB Jars to GlassFish's System Library

GlassFish has a strict classloading hierarchy, and web app-level dependencies (in WEB-INF/lib) aren't always picked up correctly. Copy the three MongoDB jars (mongodb-driver-3.6.1.jar, mongodb-driver-core-3.6.1.jar, bson-3.6.1.jar) to your GlassFish installation's domain1/lib/ext directory, then restart the server. This ensures the driver classes are available at the system classloader level.

4. Clean and Rebuild Your Deployment Package

Corrupted build artifacts can cause runtime class issues. Run these commands to wipe old builds, rebuild your project, and redeploy:

mvn clean install

Delete the existing deployment from GlassFish, upload the newly built WAR file, and restart the server to ensure fresh artifacts are used.

5. Check GlassFish-MongoDB Compatibility

Older GlassFish versions may have trouble with newer MongoDB drivers. If you're using an outdated GlassFish release, try either:

  • Upgrading GlassFish to a version that supports MongoDB driver 3.6.x
  • Downgrading your MongoDB driver to 3.4.x (a version more likely to be compatible with older GlassFish instances)

内容的提问来源于stack exchange,提问作者Chinaedu Onwukwe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:05:52