简体   繁体   English

Java jar在运行时找不到类

[英]Java jar cannot find class on run

I have created a simple Java program in IntelliJ ide. 我已经在IntelliJ ide中创建了一个简单的Java程序。 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: 我使用了很少的库,当我尝试将jar导出为工件并从命令行运行它时,出现以下错误:

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. IntelliJ编译和运行程序效果很好。

My project is configured as a gradle project with the following build.gradle : 我的项目使用以下build.gradle配置为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. 我已经按如下方式导出了jar:项目结构->工件-> Jar->从具有依赖项的模块->添加了主源文件夹和库文件中的所有库,添加了清单和主类并导出了jar。

I am trying to figure this out for days and I don't understand why it cant find the class . 我试图解决这几天,但我不明白为什么它找不到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. 当您在IDE中运行项目并且项目设置正确时, 这两件事都在类路径中。

But when you create a JAR for your project, it very much depends on your setup. 但是,当您为项目创建JAR时 ,它很大程度上取决于您的设置。 Meaning: the "default" would be that a JAR created for your project only contains the "1." 含义:“默认”是为您的项目创建的JAR 包含“ 1”。 parts. 部分。 Because you do not want that all external classes are added to "your" JAR in the first place. 因为您不希望所有外部类都首先添加到“您的” JAR中。

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! 因此,要解决此问题:当您想从命令行上的JAR运行程序时,那么您还需要所有这些外部 JAR都位于类路径中! Because, as said: to run, you need both "1" and "2" parts to be present in the classpath. 因为,如前所述:要运行,您需要在类路径中同时包含“ 1”和“ 2”部分。

Conclusion: you should check what gradle puts in the JAR it creates for you. 结论:您应该检查gradle放入它为您创建的JAR中。 I am pretty sure: that JAR only contains "1". 我很确定:JAR仅包含“ 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) (为了记录,这很好。可以将所有内容捆绑到一个jar中,但这需要您的工作)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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