Solved [Maven] Dependency Trouble

Discussion in 'Spigot Plugin Development' started by FunnyFungi, Dec 2, 2019 at 7:55 AM.

  1. I've viewed so many tutorials on how to export a Maven project as a .jar with the required dependencies, but no matter what I do, I always get a java.lang.NoClassDefFoundError error when I try to reference MongoDB.

    Here's the Error:

    Code (Text):
    Error occurred while enabling Fling v1.0.0 (Is it up to date?)

    java.lang.NoClassDefFoundError: com.mongodb.MongoClient

    at me.funnyfungi.fling.Main.onEnable(Main.java:12) ~[?:?]

    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at net.minecraft.server.v1_8_R3.MinecraftServer.s(MinecraftServer.java:414) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at net.minecraft.server.v1_8_R3.MinecraftServer.k(MinecraftServer.java:378) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at net.minecraft.server.v1_8_R3.MinecraftServer.a(MinecraftServer.java:333) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:263) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:525) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at java.lang.Thread.run(Thread.java:813) [?:1.8.0_212]

    Caused by: java.lang.ClassNotFoundException: com.mongodb.MongoClient

    at java.net.URLClassLoader.findClass(URLClassLoader.java:591) ~[?:1.8.0_212]

    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:101) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:86) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at java.lang.ClassLoader.loadClassHelper(ClassLoader.java:946) ~[?:1.8.0_212]

    at java.lang.ClassLoader.loadClass(ClassLoader.java:891) ~[?:1.8.0_212]

    at java.lang.ClassLoader.loadClass(ClassLoader.java:874) ~[?:1.8.0_212]

    ... 12 more
    Here's my pom.xml:

    Code (Text):
    <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.funnyfungi.fling</groupId>
      <artifactId>Fling</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <properties>
            <maven.compiler.source>1.6</maven.compiler.source>
            <maven.compiler.target>1.6</maven.compiler.target>
          </properties>
        <dependencies>
            <dependency>
                <groupId>org.spigotmc</groupId>
                <artifactId>spigot-api</artifactId>
                <version>1.14.4-R0.1-SNAPSHOT</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.bukkit</groupId>
                <artifactId>bukkit</artifactId>
                <version>1.14.4-R0.1-SNAPSHOT</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.mongodb</groupId>
                <artifactId>mongo-java-driver</artifactId>
                <version>3.11.2</version>
                <scope>compile</scope>
            </dependency>
        </dependencies>
        <build>
        <plugins>
            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <shadedArtifactAttached>true</shadedArtifactAttached>
                <shadedClassifierName>shaded</shadedClassifierName>
            </configuration>
            </plugin>
        </plugins>
    </build>
    </project>
    And here is my Main class:

    Code (Java):
    package me.funnyfungi.fling;

    import org.bukkit.plugin.java.JavaPlugin;

    import com.mongodb.MongoClient;
    import com.mongodb.MongoClientURI;

    public class Main extends JavaPlugin {
        @Override
        public void onEnable() {
            getLogger().info("Enabled");
            MongoClient mongoClient = new MongoClient(new MongoClientURI(mongo client URL here));
            getLogger().info("Connected to Mongo");
            mongoClient.close();
        }
    }
     

    Can someone please tell me what I'm doing wrong.
     
  2. I copied it exactly and changed the dependency, but it still threw the same error.

    Code (Text):
    Error occurred while enabling Fling v1.0.0 (Is it up to date?)

    java.lang.NoClassDefFoundError: com.mongodb.MongoClient

    at me.funnyfungi.fling.Main.onEnable(Main.java:15) ~[?:?]

    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at net.minecraft.server.v1_8_R3.MinecraftServer.s(MinecraftServer.java:414) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at net.minecraft.server.v1_8_R3.MinecraftServer.k(MinecraftServer.java:378) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at net.minecraft.server.v1_8_R3.MinecraftServer.a(MinecraftServer.java:333) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:263) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:525) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at java.lang.Thread.run(Thread.java:813) [?:1.8.0_212]

    Caused by: java.lang.ClassNotFoundException: com.mongodb.MongoClient

    at java.net.URLClassLoader.findClass(URLClassLoader.java:591) ~[?:1.8.0_212]

    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:101) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:86) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at java.lang.ClassLoader.loadClassHelper(ClassLoader.java:946) ~[?:1.8.0_212]

    at java.lang.ClassLoader.loadClass(ClassLoader.java:891) ~[?:1.8.0_212]

    at java.lang.ClassLoader.loadClass(ClassLoader.java:874) ~[?:1.8.0_212]

    ... 12 more
     
  3. Try changing you build properties to this:

    Code (Text):
    <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>2.4.3</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
     
  4. I've forgot to say, that you have to run the maven with the command: clean package
     
  5. It ran smoothly and shaded the dependencies, but now I get a new error, it seems it isn't including the plugin.yml into the .jar

    Code (Text):
    Could not load 'plugins/Fling.jar' in folder 'plugins'

    org.bukkit.plugin.InvalidDescriptionException: Invalid plugin.yml

    at org.bukkit.plugin.java.JavaPluginLoader.getPluginDescription(JavaPluginLoader.java:154) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:133) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugins(CraftServer.java:292) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:198) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:525) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

    at java.lang.Thread.run(Thread.java:813) [?:1.8.0_212]

    Caused by: java.io.FileNotFoundException: Jar does not contain plugin.yml

    ... 6 more
     
  6. Try using these build properties
    Code (Text):
        <build>
            <resources>
                <resource>
                    <targetPath>.</targetPath>
                    <filtering>true</filtering>
                    <directory>${basedir}/src/main/resources/</directory>
                </resource>
            </resources>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>3.2.1</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <filters>
                                    <filter>
                                        <excludes>
                                            <exclude>META-INF/*.SF</exclude>
                                            <exclude>META-INF/*.DSA</exclude>
                                            <exclude>META-INF/*.RSA</exclude>
                                        </excludes>
                                    </filter>
                                </filters>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    I would also recommend using Mongojack and Jackson to handle data with MongoDB

    Here is one of my projects as an example: https://github.com/InsanelyDeveloper/MongoStorage
     
  7. Edit: Nevermind I worked out the problem, this issue has now been Resolved