2

So, I have a ClassNotFoundException from my discord bot whenever I run it outside of Eclipse:

Error: A JNI error has occurred, please check your installation and try 
again
Exception in thread "main" java.lang.NoClassDefFoundError: 
sx/blah/discord/handle/obj/IChannel
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: sx.blah.discord.handle.obj.IChannel
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more

This shoud not be happening, as I have scope compile in Maven. I even explicitly put it in scope compile:

<dependencies>
    <dependency>
        <groupId>com.github.austinv11</groupId>
        <artifactId>Discord4J</artifactId>
        <version>dev-SNAPSHOT</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.16</version>
        <scope>compile</scope>
    </dependency>
</dependencies>
howlger
  • 31,050
  • 11
  • 59
  • 99
Hallowizer
  • 56
  • 8
  • Maven is a build tool. It compiles and packages your classes. But it doesn't run it. How do you run your code? – JB Nizet Sep 16 '17 at 22:13
  • java -cp JarName.jar com.hallowizer.package.Main – Hallowizer Sep 16 '17 at 22:22
  • 1
    And how did you create your jar? – Oleg Sep 16 '17 at 22:24
  • 2
    So you only put your own jar in the classpath, And not the jar it depends on. Use `java -cp jarname.jar:path/to/Discord4J.jar com.hallowizer.package.Main` (replace `:` by `;` if you're on Windows). – JB Nizet Sep 16 '17 at 22:27
  • To create/copy the dependent JARs in the target folder see https://stackoverflow.com/a/33012995/6505250 – howlger Sep 16 '17 at 22:55
  • So, is there a simpler way to do this? All I want to do here is to have a dependency in my project, without the user having to download it. – Hallowizer Sep 16 '17 at 23:55
  • I think this is what you need: https://stackoverflow.com/questions/1729054/including-dependencies-in-a-jar-with-maven - you will get a "yourproject-version-jar-with-dependencies.jar" in result which will contain every dependency besides your app. – juzraai Sep 17 '17 at 10:59

1 Answers1

2

Apache Maven is a build tool for,

  • Retrieving project dependencies (3rd party Jars, etc.) from remote repositories to the local machine based on the dependencies declared in the pom.xml file.
  • Compiling Java classes

In your case, your pom.xml fetched all the dependencies and put them on your local computer. Eclipse's built-in Maven plugin puts those Jar files in your project's classpath. This is the reason, you were able to run your application within Eclipse IDE.

If you wish to run your application outside Eclipse IDE, you can do it in a couple of ways:

  1. Put all the dependencies (JARs) in your application classpath, e.g,

    $ java -cp ".:./jars/a.jar; ../jars/b.jar" myClass

  2. Create a uber jar by using Maven Shade plugin. Here is the documentation on using the Shade plugin.
Indra Basak
  • 7,124
  • 1
  • 26
  • 45