0

I'm trying to make a discord bot in Java using Javacord. I made my code, built it with Gradle and typed cmd command: java -jar botgamin.jar. I got this:

Exception in thread "main" java.lang.NoClassDefFoundError: org/javacord/api/DiscordApiBuilder
        at me.maxisy.gamingbot.Main.main(Main.java:10)
Caused by: java.lang.ClassNotFoundException: org.javacord.api.DiscordApiBuilder
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more

Could someone help me? Here is my code: https://pasteboard.co/Jfuc7yN.png

Vampire
  • 35,631
  • 4
  • 76
  • 102
Maxisy
  • 50
  • 1
  • 6
  • Does this answer your question? [Why am I getting a NoClassDefFoundError in Java?](https://stackoverflow.com/questions/34413/why-am-i-getting-a-noclassdeffounderror-in-java) – Minn Jun 30 '20 at 13:41

2 Answers2

1

It seems its not getting the imported class DiscordApiBuilder while trying execute. try giving class path while executing. In order to do that, try the following.

Firstly, Put the jar for org.javacord.api(jar in which DiscordApiBuilder class exist) in the same location from which you are trying to execute the commenad.

Secondly, edit the command and try below one,

# Note : assuming DiscordApiBuilder jar name is DiscordApiBuilder.jar
# -cp : this indicate Class Path. hence it will loop for the imports in the mentioned jar as well.

java -cp DiscordApiBuilder.jar -jar botgamin.jar

Hope this helps.

Jithin Scaria
  • 1,271
  • 1
  • 15
  • 26
1

From what I read that error is caused by a difference in class-paths from compile-time to run-time, after a lot of digging I found out that adding

jar { 
    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

to my build.gradle fixed the issue, this I believe packs the classpath into the jar file, so at runtime it can actually find it, correct me if I'm wrong though. But this did seem to fix the problem for me

Beyley
  • 11
  • 2