简体   繁体   中英

Java jar cannot find class on run

I have created a simple Java program in IntelliJ ide. I used few libraries and when I try to export the jar as an artifact and run it from command line I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/util/concurrent/FutureCallback
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Class.java:2615)
        at java.lang.Class.getMethod0(Class.java:2856)
        at java.lang.Class.getMethod(Class.java:1668)
        at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: com.google.common.util.concurrent.FutureCallback
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        ... 6 more

Compiling & Running the program inside IntelliJ works great.

My project is configured as a gradle project with the following build.gradle :

group 'marianpavel.com'
version '1.0'

apply plugin: 'java'

sourceCompatibility = 1.5

repositories {
    mavenCentral()
    maven {
        url "http://repo.bastian-oppermann.de"
    }
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile 'de.btobastian.javacord:javacord:2.0.11'
    compile 'ch.qos.logback:logback-classic:1.0.13'
    compile 'de.btobastian.sdcf4j:sdcf4j-core:1.0.2'
    compile 'de.btobastian.sdcf4j:sdcf4j-javacord:1.0.2'
    compile 'org.jsoup:jsoup:1.9.2'
}

I have exported the jar as followed: Project Structure -> Artifacts -> Jar -> From Module with dependencies -> Added the main source folder and all the libraries from library files, added a manifest and a main class and exported the jar.

I am trying to figure this out for days and I don't understand why it cant find the class .

I think there is a misconception on your end.

Your project consists of two important parts:

  1. Your own code
  2. External libraries that your code is using

When you run your project within an IDE, and your project setup is correct, then both things are in your classpath.

But when you create a JAR for your project, it very much depends on your setup. Meaning: the "default" would be that a JAR created for your project only contains the "1." parts. Because you do not want that all external classes are added to "your" JAR in the first place.

So, to resolve that: when you want to run your program from your JAR on the command line, then you will need all those external JARs to be in your classpath as well! Because, as said: to run, you need both "1" and "2" parts to be present in the classpath.

Conclusion: you should check what gradle puts in the JAR it creates for you. I am pretty sure: that JAR only contains "1".

(and for the record, it is fine like that. it is possible to bundle everything into a single jar, but that will require work on your end)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM