0

I have a Discord Bot (in Java) and everything is good if I run the bot directly in IntelliJ (Maven) But if I want to build the Bot in a JAR I get this error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/MongoClient
        at de.therealjan.TheRealBot.MongoDB.<init>(MongoDB.java:14)
        at de.therealjan.TheRealBot.TheRealBot.main(TheRealBot.java:16)
Caused by: java.lang.ClassNotFoundException: com.mongodb.MongoClient
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) 

MongoDB.java:

package de.therealjan.TheRealBot;

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

import java.net.UnknownHostException;

public class MongoDB {
    public MongoClient mongoClient;
    public DB db;
    public MongoDB() {
        try {
            mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        db = mongoClient.getDB("TheRealBot-JDA");
    }
}

I think Maven didn't built it right, but I have no idea. (I ran the package Maven Build)

TheRealJan
  • 49
  • 1
  • 6
  • Maven, by default, doesn't include the dependencies - you need to provide a custom build step(s) to include the dependencies based on your desired needs. It's been a while since I used maven, but [something like this](https://stackoverflow.com/questions/24899985/this-project-cannot-be-added-because-it-does-not-produce-a-jar-file-using-an-ant/24900260#24900260) might lead towards a solution – MadProgrammer May 06 '20 at 09:37
  • You can review my answer [here](https://stackoverflow.com/a/56288962/9381897). It refers to another dependency but the steps are the same. – leopal May 06 '20 at 09:46

1 Answers1

1

Maven by default doesn't package your dependencies and their transitive dependencies inside the build artifacts. This doesn't cause any issues in the compile time as the dependent classes are there in the IDE's libs classpath. But in the runtime, this might throw a ClassNotFoundException exception as those classes are not in the runtime classpath. You have to instruct maven to package those inside your jar.

Add this plugin in your Maven pom.xml file.

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <id>jar-with-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                            <archive>
                                <manifest>
                                    <mainClass>YOUR_MAIN_CLASS_FQN_GOES_HERE</mainClass>
                                </manifest>
                            </archive>
                        </configuration>
                    </execution>
                  </executions>
            </plugin>
        </plugins>
    </build>

Make sure that you define your main class as mentioned.

Nipun Thathsara
  • 1,119
  • 11
  • 20