0

I'm creating a Minecraft Java plugin, but I have an error and I cannot find the solution despite many attempts. I tried to shade, to compile the .jar with the dependencies and more. I don't know what to do.

The error is "java.lang.NoClassDefFoundError: com/mongodb/MongoClientURI".

Here is the error

[16:12:16 ERROR]: Error occurred while enabling BattlePot v1.0 (Is it up to date?) java.lang.NoClassDefFoundError: com/mongodb/MongoClientURI
    at ca.lwi.battlepot.mongo.MongoConnection.init(MongoConnection.java:21) ~[?:?]
    at ca.lwi.battlepot.mongo.manager.MongoManager.registerDatabase(MongoManager.java:18) ~[?:?]
    at ca.lwi.battlepot.mongo.manager.MongoManager.<init>(MongoManager.java:13) ~[?:?]
    at ca.lwi.battlepot.BP.onEnable(BP.java:32) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.s(MinecraftServer.java:414) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.k(MinecraftServer.java:378) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.a(MinecraftServer.java:333) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:263) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:525) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_311]                          
Caused by: java.lang.ClassNotFoundException: com.mongodb.MongoClientURI
    at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_311]
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:101) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:86) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_311]
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_311]
    ... 15 more

Here is my pom.xml file:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>8</source>
                <target>8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.4</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <createDependencyReducedPom>false</createDependencyReducedPom>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>fully.qualified.MainClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

<repositories>
    <!-- Spigot Maven -->
    <repository>
        <id>spigot-repo</id>
        <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
    </repository>
    <repository>
        <id>sonatype</id>
        <url>https://oss.sonatype.org/content/groups/public/</url>
    </repository>
</repositories>

<dependencies>
    <!-- Spigot API -->
    <dependency>
        <groupId>org.spigotmc</groupId>
        <artifactId>spigot-api</artifactId>
        <version>1.8.8-R0.1-SNAPSHOT</version>
        <scope>provided</scope>
    </dependency>

    <!-- MongoDB -->
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>3.12.10</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

Sorry for my bad english. It's not my first language.

Lwi
  • 1
  • 2
  • Have you check that the Mongodb code was well on the given jar ? Also, MySQL is already include in JDK, So I suggest you to use Mysql's default driver **if you can** – Elikill58 Nov 07 '21 at 22:41
  • @Elikill58 Yep. I used it in class – Lwi Nov 07 '21 at 22:51
  • You can include the code that is throwing the exception. Also see [Why am I getting a NoClassDefFoundError in Java?](https://stackoverflow.com/questions/34413/why-am-i-getting-a-noclassdeffounderror-in-java). – prasad_ Nov 08 '21 at 05:24
  • @Lwi I mean, if in the compiled jar. Here you said that maven is enough config to use it but it seems to don't be included in the exported jar – Elikill58 Nov 08 '21 at 10:54

1 Answers1

1

Later Mongo Driver removed support for MongoClientURI, but you can find it with this dependency

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongodb-driver-legacy</artifactId>
    <version>3.12.10</version>
    <scope>compile</scope>
</dependency>
AndrewL
  • 2,034
  • 18
  • 18