4

I am trying to build a discord bot with the Java Discord API, but when I run this code snippet:

public static void main(String[] args) throws LoginException {
    JDABuilder builder = JDABuilder.createDefault(token);
    builder.setActivity(Activity.watching("boop"));
    builder.build();
}

It throws this exception:

Exception in thread "main" java.lang.NoClassDefFoundError: gnu/trove/map/TLongObjectMap
at net.dv8tion.jda@4.2.0_216/net.dv8tion.jda.api.entities.Activity.watching(Activity.java:204)
at rambot/rambot.discord.MainApp.main(MainApp.java:14)Caused by: java.lang.ClassNotFoundException: gnu.trove.map.TLongObjectMap
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 2 more

Why is this being thrown, and how can I fix it?

a.deshpande012
  • 715
  • 7
  • 18
  • What version of JDA are you using? – Cardinal System Nov 16 '20 at 21:13
  • I'm using JDA-4.2.0_217 – BENRABAH Brahim Zaky Nov 16 '20 at 21:54
  • Interesting. There is no code on [line 204 in Activity.java](https://github.com/DV8FromTheWorld/JDA/blob/master/src/main/java/net/dv8tion/jda/api/entities/Activity.java#L204), nor is `TLongObjectMap` present anywhere in the class. Have you tried adding [Trove4J](https://bitbucket.org/trove4j/trove/src/master/) to your `pom.xml`/`build.gradle`/`.classpath`? I assume you are using a linux machine? – Cardinal System Nov 16 '20 at 22:03
  • Well,I found a solution ^^',by going to the [JDA github] (https://github.com/DV8FromTheWorld/JDA/wiki/19)-Troubleshooting#noclassdeffounderror-or-classnotfoundexception-on-startup) ,If the code didn't work it's because I wasn't with the -withDependencies.jar now I have no more problems, thank you very much for reading my post, I should have inquired a little more before asking for help – BENRABAH Brahim Zaky Nov 16 '20 at 22:54
  • In the future, you may want to use build tools like gradle or maven. They will automatically implement all those dependencies for you. – Cardinal System Nov 17 '20 at 01:39

2 Answers2

0

For people who see this, but can't seem to read the comments or figure it out on their own. On the JDA wiki, there is an explanation as well. Credits to @BENRABAH Brahim Zaky

NoClassDefFoundError or ClassNotFoundException on startup
An error like java.lang.NoClassDefFoundError: net/dv8tion/jda/api/JDABuilder or similar means you are not including your dependencies or transitive dependencies in the archive.

Gradle (build.gradle)
With gradle this can be fixed by using the shadow plugin and building your jar with shadowJar instead. The jar will then be present in the build/libs directory with a name like example-1.0-all.jar

Maven (pom.xml)
With maven you need the shade plugin in your pom to add dependencies to your package task. You can see the shade plugin being applied in this example pom.xml

Jar (No build tools)
You need to use the -withDependencies.jar rather than the normal one.

The preferred method depends per programmer, but I suggest using either gradle or maven. Since they do everything from compiling your Jar to adding the dependencies.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
stefano
  • 134
  • 7
0

I had the same issue too, this error is caused since your compiler didn't found the class path

my solution:

you have to delete the (JDA) from Modulepath and add it to Classpathh inside the (java build path).

steve
  • 1