Getting NoClassDef with MongoDB driver

I’m currently working on a Java spigot plugin and getting a NoClassDefError with my MongoDB. I tried almost everything, but still can’t fix it.

My pom.xml: https://pastebin.com/W2iATcPx
My code: https://pastebin.com/TFFxt7jE
Maven classes:

Hi @dlabaja,

3.9.1 is a bit old.
Can you try 4.2.3 instead?

You can check out this repo which is also using the sync driver and works without issues: https://github.com/mongodb-developer/java-quick-start

If that doesn’t solve your issue, I guess you have a path issue somewhere.

Cheers,
Maxime.

Hi, thanks for the quick response. I updated the driver and copied my source code to the project you sent, but I’m still getting the same error.

log: https://pastebin.com/xgZx6SW3
project (the new one I made from your repo): https://drive.google.com/drive/folders/141vTJ4nVdVdM6Xmcg9ASG9NQ6H3U0XyB?usp=sharing

$ mvn clean compile 
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------------< me.dlabaja:dbl >---------------------------
[INFO] Building dblPlugin 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
Downloading from sonatype: https://oss.sonatype.org/content/groups/public/org/spigotmc/spigot-api/1.16.5-R0.1-SNAPSHOT/maven-metadata.xml
Downloading from spigotmc-repo: https://hub.spigotmc.org/nexus/content/repositories/snapshots/org/spigotmc/spigot-api/1.16.5-R0.1-SNAPSHOT/maven-metadata.xml
Downloaded from spigotmc-repo: https://hub.spigotmc.org/nexus/content/repositories/snapshots/org/spigotmc/spigot-api/1.16.5-R0.1-SNAPSHOT/maven-metadata.xml (1.5 kB at 1.9 kB/s)
Downloading from spigotmc-repo: https://hub.spigotmc.org/nexus/content/repositories/snapshots/org/spigotmc/spigot-api/1.16.5-R0.1-SNAPSHOT/spigot-api-1.16.5-R0.1-20210519.225132-83.pom
Downloaded from spigotmc-repo: https://hub.spigotmc.org/nexus/content/repositories/snapshots/org/spigotmc/spigot-api/1.16.5-R0.1-SNAPSHOT/spigot-api-1.16.5-R0.1-20210519.225132-83.pom (10 kB at 30 kB/s)
Downloading from spigotmc-repo: https://hub.spigotmc.org/nexus/content/repositories/snapshots/com/google/guava/guava/21.0/guava-21.0.pom
Downloading from sonatype: https://oss.sonatype.org/content/groups/public/com/google/guava/guava/21.0/guava-21.0.pom
Downloaded from sonatype: https://oss.sonatype.org/content/groups/public/com/google/guava/guava/21.0/guava-21.0.pom (7.0 kB at 13 kB/s)
Downloading from spigotmc-repo: https://hub.spigotmc.org/nexus/content/repositories/snapshots/com/google/guava/guava-parent/21.0/guava-parent-21.0.pom
Downloading from sonatype: https://oss.sonatype.org/content/groups/public/com/google/guava/guava-parent/21.0/guava-parent-21.0.pom
Downloaded from sonatype: https://oss.sonatype.org/content/groups/public/com/google/guava/guava-parent/21.0/guava-parent-21.0.pom (9.7 kB at 44 kB/s)
Downloading from spigotmc-repo: https://hub.spigotmc.org/nexus/content/repositories/snapshots/net/md-5/bungeecord-chat/1.16-R0.4/bungeecord-chat-1.16-R0.4.pom
Downloading from sonatype: https://oss.sonatype.org/content/groups/public/net/md-5/bungeecord-chat/1.16-R0.4/bungeecord-chat-1.16-R0.4.pom
Downloaded from sonatype: https://oss.sonatype.org/content/groups/public/net/md-5/bungeecord-chat/1.16-R0.4/bungeecord-chat-1.16-R0.4.pom (978 B at 4.9 kB/s)
Downloading from spigotmc-repo: https://hub.spigotmc.org/nexus/content/repositories/snapshots/net/md-5/bungeecord-parent/1.16-R0.4/bungeecord-parent-1.16-R0.4.pom
Downloading from sonatype: https://oss.sonatype.org/content/groups/public/net/md-5/bungeecord-parent/1.16-R0.4/bungeecord-parent-1.16-R0.4.pom
Downloaded from sonatype: https://oss.sonatype.org/content/groups/public/net/md-5/bungeecord-parent/1.16-R0.4/bungeecord-parent-1.16-R0.4.pom (10 kB at 51 kB/s)
Downloading from spigotmc-repo: https://hub.spigotmc.org/nexus/content/repositories/snapshots/org/spigotmc/spigot-api/1.16.5-R0.1-SNAPSHOT/spigot-api-1.16.5-R0.1-20210519.225132-83.jar
Downloading from spigotmc-repo: https://hub.spigotmc.org/nexus/content/repositories/snapshots/net/md-5/bungeecord-chat/1.16-R0.4/bungeecord-chat-1.16-R0.4.jar
Downloading from spigotmc-repo: https://hub.spigotmc.org/nexus/content/repositories/snapshots/com/google/guava/guava/21.0/guava-21.0.jar
Downloaded from spigotmc-repo: https://hub.spigotmc.org/nexus/content/repositories/snapshots/org/spigotmc/spigot-api/1.16.5-R0.1-SNAPSHOT/spigot-api-1.16.5-R0.1-20210519.225132-83.jar (1.3 MB at 3.9 MB/s)
Downloading from sonatype: https://oss.sonatype.org/content/groups/public/com/google/guava/guava/21.0/guava-21.0.jar
Downloading from sonatype: https://oss.sonatype.org/content/groups/public/net/md-5/bungeecord-chat/1.16-R0.4/bungeecord-chat-1.16-R0.4.jar
Downloaded from sonatype: https://oss.sonatype.org/content/groups/public/com/google/guava/guava/21.0/guava-21.0.jar (2.5 MB at 6.3 MB/s)
Downloaded from sonatype: https://oss.sonatype.org/content/groups/public/net/md-5/bungeecord-chat/1.16-R0.4/bungeecord-chat-1.16-R0.4.jar (149 kB at 311 kB/s)
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ dbl ---
[INFO] Deleting /tmp/java/java-quick-start-master/java-quick-start-master/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ dbl ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ dbl ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 5 source files to /tmp/java/java-quick-start-master/java-quick-start-master/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  9.929 s
[INFO] Finished at: 2021-05-20T13:59:33+02:00
[INFO] ------------------------------------------------------------------------

Looks like I can compile it without issues.

But there is no main method to run your code, so I have no idea how to run it.
I added a FakeMain class in your code base to see if I could run it:

package me.dlabaja.dbl;

import com.mongodb.ConnectionString;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import org.bson.Document;

import java.util.ArrayList;
import java.util.List;

public class FakeMain {

    public static void main(String[] args) {
        try (MongoClient mongoClient = MongoClients.create(new ConnectionString("mongodb://localhost"))) {
            List<Document> databases = mongoClient.listDatabases().into(new ArrayList<>());
            databases.forEach(db -> System.out.println(db.toJson()));
        }
    }

}

And it ran without issues

May 20, 2021 2:19:01 PM com.mongodb.diagnostics.logging.Loggers shouldUseSLF4J
WARNING: SLF4J not found on the classpath.  Logging is disabled for the 'org.mongodb.driver' component
{"name": "admin", "sizeOnDisk": 81920.0, "empty": false}
{"name": "config", "sizeOnDisk": 28672.0, "empty": false}
{"name": "local", "sizeOnDisk": 356352.0, "empty": false}

Just the SLF4J warning.

I have no idea how you are actually using and running this code. But the problem comes from the packaging I guess because I’m guessing you are using this as a jar file. The mongodb driver is probably not packaged correctly within this jar. So it compiles in your IDE, but the class is missing at runtime.

Also, on a side note, I find this very suspicious that you have to create a new MongoClient each time a players joins the world. I think it would be better to have only a single instanciation of it for the entire application and not create a new one each time an event happens. Much better performances I guess.

Cheers,
Maxime.

Hi, thanks again for a response.
I’m running this as a plugin, so instead of main() there is onEnable(). Then I’m exporting it into my plugins folder, where it starts together with my server.

Plugins folder and inside the jar:

I think you are true about missing due to compile because I can’t find any MongoDB classes in the exported jar.

Your JAR packager should be configured to be a fat JAR with all the dependencies in it.
Usually I use maven-assembly-plugin for this.

Thank you very much, it works!!!

Just a final question, can I build this directly to the plugin folder?

No idea :frowning:. I don’t know enough about the project itself :slight_smile:. But it it’s driven by maven as well, I don’t see why not.

1 Like

Just found a way to do it, you can add dependencies manually in modules

Thanks for your help, I’m really grateful for it, and I’m not sure what would I do without you.

Isn’t this supposed to be managed by the pom.xml file?

I don’t know, the main thing is that it’s working :smiley:

1 Like

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