0

I'm making a minecraft plugin for my minecraft server but I have an error that I can't found the solution. The context: I want to store the data of a player like level/xp/rank etc... Can you help me to do this part of the plugin, I'm a beginner in Java.

This is my code :

import com.mongodb.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import me.codexis.velocitylobbygeneral.commands.Lobby;
import me.codexis.velocitylobbygeneral.commands.MoveBot;
import me.codexis.velocitylobbygeneral.commands.Test;
import me.codexis.velocitylobbygeneral.event.*;
import org.bson.Document;
import org.bukkit.ChatColor;
import org.bukkit.plugin.java.JavaPlugin;

public final class VelocityLobbyGeneral extends JavaPlugin {

    private static VelocityLobbyGeneral instance;

    @Override
    public void onEnable() {
        setInstance(this);

        // Listeners
        getServer().getPluginManager().registerEvents(new OnJoinQuit(), this);
        getServer().getPluginManager().registerEvents(new FormatChat(), this);
        getServer().getPluginManager().registerEvents(new Scoreboard(), this);

        // Channels
        getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");

        // Commands
        new Lobby();
        new MoveBot();
        new Test();

        // Connection to database
        MongoClient mongoClient = (MongoClient) MongoClients.create("mongodb+srv://myusername:@databasemc.ehssc.mongodb.net/VelocityMC?retryWrites=true&w=majority");
        MongoDatabase mongoDatabase = mongoClient.getDatabase("VelocityMC");
        MongoCollection<Document> mongoCollection = mongoDatabase.getCollection("Vide");
        getLogger().info(ChatColor.GREEN + "Connected to Database");

        getLogger().info("=============================================");
        getLogger().info("   >>>> Velocity Lobby General Loaded <<<<   ");
        getLogger().info("=============================================");
   }

   @Override
   public void onDisable() {

       getLogger().info("===============================================");
       getLogger().info("   >>>> Velocity Lobby General disabled <<<<   ");
       getLogger().info("===============================================");

   }

   public static VelocityLobbyGeneral getInstance(){
       return instance;
   }

   private static void setInstance(VelocityLobbyGeneral instance){
      velocityLobbyGeneral.instance = instance;
   }

}

And this is my error :

[12:32:49 WARN]: java.lang.NoClassDefFoundError: com/mongodb/client/MongoClients
[12:32:49 WARN]:        at VelocityLobbyGeneral.jar//me.codexis.velocitylobbygeneral.VelocityLobbyGeneral.onEnable(VelocityLo 
bbyGeneral.java:38)
[12:32:49 WARN]:        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264)
[12:32:49 WARN]:        at 
org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:370)
[12:32:49 WARN]:        at 
org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:500)
[12:32:49 WARN]:        at 
org.bukkit.craftbukkit.v1_17_R1.CraftServer.enablePlugin(CraftServer.java:535)
[12:32:49 WARN]:        at 
org.bukkit.craftbukkit.v1_17_R1.CraftServer.enablePlugins(CraftServer.java:449)
[12:32:49 WARN]:        at 
org.bukkit.craftbukkit.v1_17_R1.CraftServer.reload(CraftServer.java:970)
[12:32:49 WARN]:        at org.bukkit.Bukkit.reload(Bukkit.java:769)
[12:32:49 WARN]:        at         org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:54)
[12:32:49 WARN]:        at     org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:159)
[12:32:49 WARN]:        at org.bukkit.craftbukkit.v1_17_R1.CraftServer.dispatchCommand(CraftServer.java:838)
[12:32:49 WARN]:        at     org.bukkit.craftbukkit.v1_17_R1.CraftServer.dispatchServerCommand(CraftServer.java:801)
[12:32:49 WARN]:        at net.minecraft.server.dedicated.DedicatedServer.handleCommandQueue(DedicatedServer.java:518)
[12:32:49 WARN]:        at     net.minecraft.server.dedicated.DedicatedServer.b(DedicatedServer.java:480)
[12:32:49 WARN]:        at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:1475)
[12:32:49 WARN]:        at net.minecraft.server.MinecraftServer.x(MinecraftServer.java:1274)
[12:32:49 WARN]:        at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:319)
[12:32:49 WARN]:        at java.base/java.lang.Thread.run(Thread.java:831)
[12:32:49 WARN]: Caused by: java.lang.ClassNotFoundException: com.mongodb.client.MongoClients
[12:32:49 WARN]:        at org.bukkit.plugin.java.PluginClassLoader.loadClass0(PluginClassLoader.java:146)
[12:32:49 WARN]:        at org.bukkit.plugin.java.PluginClassLoader.loadClass(PluginClassLoader.java:103)
[12:32:49 WARN]:        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:519)
[12:32:49 WARN]:        ... 18 more

Please can anyone help me.

Pom.xml :

<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>me.codexisphantom</groupId>
<artifactId>VelocityLobby</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

<name>VelocityLobby</name>

<description>Official VelocityMC Plugin</description>
<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<url>www.velocity-net.com</url>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</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>
    </plugins>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

<repositories>
    <repository>
        <id>papermc-repo</id>
        <url>https://papermc.io/repo/repository/maven-public/</url>
    </repository>
    <repository>
        <id>sonatype</id>
        <url>https://oss.sonatype.org/content/groups/public/</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>io.papermc.paper</groupId>
        <artifactId>paper-api</artifactId>
        <version>1.17.1-R0.1-SNAPSHOT</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

Mongodb dependency :

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

Plugin added :

    <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>
Codexis
  • 11
  • 6

1 Answers1

0

In your maven configuration, you are importing MongoDB. So, it will compile and you will be able to use it when you will develop.

But, when you will compile, it will not be included in final jar. And such as MongoDB isn't in spigot, it will create your error (don't find MongoDB class).

To fix it, there is multiple tutorial 1, 2, 3, 4 ... and I'm sure we can find more.

I suggest you to import mongo db like that :

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>3.12.10</version> <!-- The version that you want -->
    <scope>compile</scope> <!-- In my maven project, it include this project in builded jar -->
</dependency>

You can find all versions of MongoDB here

My full config which works :

<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
 
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</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>
    </plugins>
   </build>

 <repositories>
   <repository>
       <id>sonatype</id>
       <url>https://oss.sonatype.org/content/groups/public/</url>
   </repository>
 </repositories>

 <dependencies>
     <dependency>
       <groupId>org.mongodb</groupId>
       <artifactId>mongo-java-driver</artifactId>
       <version>3.12.10</version>
       <scope>compile</scope>
   </dependency>
 </dependencies>
Elikill58
  • 4,050
  • 24
  • 23
  • 45