JDA Discord bot fails with ClassNotFoundException: How to resolve?

Hey everyone, I’m new to making Discord bots with JDA. I’m trying to get a simple bot up and running, but I’m hitting a roadblock. Here’s what I’ve got:

public class BotLauncher {
    public static void main(String[] args) throws Exception {
        String botToken = "your-token-here";
        JDABuilder jdaBuilder = JDABuilder.createDefault(botToken);
        jdaBuilder.setActivity(Activity.watching("for commands"));
        jdaBuilder.build();
    }
}

When I run this, I get a ClassNotFoundException for gnu/trove/map/TLongObjectMap. The error trace mentions something about the Activity class.

I’m scratching my head here. What’s going wrong? Is there a dependency I’m missing? How can I get this bot to start up properly?

Any help would be super appreciated. Thanks!

I’ve run into this issue before while working on my own Discord bot. The ClassNotFoundException you’re encountering is typically caused by a missing dependency. In my experience, adding the Trove4j library to your project should resolve the problem.

When I faced this, I was using Gradle. I simply added the following line to my dependencies block:

implementation ‘net.sf.trove4j:trove4j:3.0.3’

After syncing the project, the error disappeared. If you’re using Maven instead, you’ll need to add the corresponding XML to your pom.xml file.

One thing to keep in mind: make sure you’re using a compatible version of JDA with your other dependencies. I’ve found that version mismatches can sometimes cause unexpected issues like this. It might be worth double-checking your JDA version and ensuring it’s up to date.

Hope this helps you get your bot up and running!

hey man, ive had that issue before. try adding the trove4j dependency to ur project. its like net.sf.trove4j:trove4j:3.0.3 or somthing. that should fix the ClassNotFoundException. good luck with ur bot!

It sounds like you’re missing a crucial dependency in your project. The gnu/trove/map/TLongObjectMap class is part of the Trove library, which JDA uses internally. To resolve this, you need to add the Trove4j dependency to your project.

If you’re using Maven, add this to your pom.xml:

<dependency>
    <groupId>net.sf.trove4j</groupId>
    <artifactId>trove4j</artifactId>
    <version>3.0.3</version>
</dependency>

For Gradle, add this to your build.gradle:

implementation 'net.sf.trove4j:trove4j:3.0.3'

After adding this dependency and refreshing your project, the ClassNotFoundException should be resolved. Make sure you’re using the latest version of JDA as well, as older versions might have different dependency requirements.