Solved MongoDB Maven java.lang.NoClassDefFoundError: com/mongodb/ConnectionString

Discussion in 'Spigot Plugin Development' started by Prifabu, Jun 5, 2018.

  1. Code (Text):
    [21:26:49 ERROR]: Error occurred while enabling sEssentials v1.0 (Is it up to date?)
    java.lang.NoClassDefFoundError: com/mongodb/ConnectionString
        at de.sEssentials.Managers.MongoManager.connect(MongoManager.java:34) ~[?:?]
        at de.sEssentials.main.Main.onEnable(Main.java:35) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[spigot.jar:git-Spigot-2086bb0-8cc5a7e]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [spigot.jar:git-Spigot-2086bb0-8cc5a7e]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:403) [spigot.jar:git-Spigot-2086bb0-8cc5a7e]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:381) [spigot.jar:git-Spigot-2086bb0-8cc5a7e]
        at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:330) [spigot.jar:git-Spigot-2086bb0-8cc5a7e]
        at net.minecraft.server.v1_12_R1.MinecraftServer.t(MinecraftServer.java:422) [spigot.jar:git-Spigot-2086bb0-8cc5a7e]
        at net.minecraft.server.v1_12_R1.MinecraftServer.l(MinecraftServer.java:383) [spigot.jar:git-Spigot-2086bb0-8cc5a7e]
        at net.minecraft.server.v1_12_R1.MinecraftServer.a(MinecraftServer.java:338) [spigot.jar:git-Spigot-2086bb0-8cc5a7e]
        at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:272) [spigot.jar:git-Spigot-2086bb0-8cc5a7e]
        at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:545) [spigot.jar:git-Spigot-2086bb0-8cc5a7e]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_121]
    Caused by: java.lang.ClassNotFoundException: com.mongodb.ConnectionString
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[?:1.8.0_121]
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:152) ~[spigot.jar:git-Spigot-2086bb0-8cc5a7e]
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:100) ~[spigot.jar:git-Spigot-2086bb0-8cc5a7e]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[?:1.8.0_121]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[?:1.8.0_121]
        ... 13 more
     
    Code (Java):
    package de.sEssentials.Managers;

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

    import java.text.MessageFormat;

    /**
    * Created on 04.Jun 2018 21:43.
    * Created by Prifabu.
    * sEssentials
    */


    public class MongoManager {

        private final String hostname;
        private final int port;

        private MongoClient client;
        private MongoDatabase database;

        private MongoCollection<Document> players;

        public MongoManager(String hostname, int port) {
            this.hostname = hostname;
            this.port = port;
        }

        public void connect() {
    /* LINE 34 */        this.client = MongoClients.create(new ConnectionString("mongodb://" + hostname + ":" + port));
            this.database = this.client.getDatabase("sEssentials");
            this.players = this.database.getCollection("players");
        }

        public void connect(String username, String password, String database) {
            this.client = MongoClients.create(new ConnectionString(MessageFormat.format("mongodb://{0}:{1}@{2}:{3}/{4}", username, password, hostname, port, database)));

            this.database = this.client.getDatabase("sEssentials");
            this.players = this.database.getCollection("players");
        }

        public MongoCollection<Document> getPlayers() {
            return players;
        }
    }
     
    Code (Text):
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>

        <groupId>sEssentials</groupId>
        <artifactId>sEssentials</artifactId>
        <version>1.0-SNAPSHOT</version>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>


        <repositories>
            <repository>
                <id>spigot-repo</id>
                <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
            </repository>
        </repositories>
        <dependencies>
            <!--Spigot API-->
            <dependency>
                <groupId>org.spigotmc</groupId>
                <artifactId>spigot-api</artifactId>
                <version>1.12.2-R0.1-SNAPSHOT</version>
                <scope>provided</scope>
            </dependency>
            <!--Bukkit API-->
            <dependency>
                <groupId>org.bukkit</groupId>
                <artifactId>bukkit</artifactId>
                <version>1.12.2-R0.1-SNAPSHOT</version>
                <scope>provided</scope>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
            <dependency>
                <groupId>org.mongodb</groupId>
                <artifactId>mongodb-driver-async</artifactId>
                <version>3.7.0-rc0</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.mongodb/bson -->
            <dependency>
                <groupId>org.mongodb</groupId>
                <artifactId>bson</artifactId>
                <version>3.7.0-rc0</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.mongodb/mongodb-driver-core -->
            <dependency>
                <groupId>org.mongodb</groupId>
                <artifactId>mongodb-driver-core</artifactId>
                <version>3.7.0-rc0</version>
            </dependency>
        </dependencies>
    </project>

    Somebody is able to find out what i am doing wrong? ^^
     
  2. need to use maven shade plugin
     
    • Useful Useful x 1
  3. You need to set the scope of the MongoDB dependency to <scope>compile<\scope> and use the Maven Shade plugin to shade your dependency into your plugin. An example can be found here.
     
    • Useful Useful x 1
    • Useful Useful x 1
  4. Thanks for the quick Help! :)
    Working fine now!