【问题标题】:Error in executing the Jar file in command Pompt (Gradle Build): NoClassDefFoundError: org/apache/poi/xssf/usermodel/XSSFWorkbook在命令 Pompt (Gradle Build) 中执行 Jar 文件时出错:NoClassDefFoundError: org/apache/poi/xssf/usermodel/XSSFWorkbook
【发布时间】:2018-01-08 21:30:12
【问题描述】:

我正在使用 Gradle Build 构建一个 Java 项目。该项目有一些外部依赖项,如 Apache POI、Spring Core/Context、Log4j。当我在 IDE 中运行该项目时,它正在成功编译和执行,没有任何错误。 但是,当我使用 gradle build 构建 Jar 文件并在命令提示符下运行它(使用“java -jar 文件名”)时,我得到错误 -

NoClassDefFoundError: org/apache/poi/xssf/usermodel/XSSFWorkbook

这是我的 build.gradle 文件 -

buildscript{
    repositories{
        mavenCentral()
    }

    dependencies{
        classpath group: 'org.springframework', name: 'spring-core', version: '5.0.2.RELEASE'
        classpath group: 'org.springframework', name: 'spring-context', version: '5.0.2.RELEASE'
        classpath group: 'org.apache.poi', name: 'poi', version: '3.17'
        classpath group: 'org.apache.poi', name: 'poi-ooxml', version: '3.17'
        classpath group: 'org.apache.poi', name: 'poi-ooxml-schemas', version: '3.17'
        classpath group: 'org.apache.commons', name: 'commons-collections4', version: '4.1'
        classpath group: 'org.apache.xmlbeans', name: 'xmlbeans', version: '2.6.0'
    }
 }

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

mainClassName = '<package>.HelloWorld'

dependencies{

    compile 'org.apache.poi:poi:3.17'
    compile 'org.apache.poi:poi-ooxml:3.17'
    compile 'org.apache.poi:poi-ooxml-schemas:3.17'
    compile 'org.springframework:spring-core:5.0.2.RELEASE'
    compile 'org.springframework:spring-context:5.0.2.RELEASE'
    compile 'org.apache.commons:commons-collections4:4.1'
    compile 'org.apache.xmlbeans:xmlbeans:2.6.0'

}

jar {
    manifest{
        attributes '<package>.HelloWorld'
    }
    baseName = 'finder-app'
    version =  '0.1.0'
}

我的本​​地机器上存在依赖 jar 文件。

使用build.gradle打包jar时需要添加jar文件吗?如果是,那么如何?或

我需要将 CLASSPATH(到外部 jar 目录)添加到我的本地机器环境变量吗?

【问题讨论】:

  • 你正在执行什么命令来运行 jar ?
  • java -jar finder-app-0.1.0.jar
  • 在您的 jar META-INF/manifest 中,类路径设置是否正确?
  • 我的清单文件可能有问题。不过不确定。这是 META-INF/manifest 文件的内容(它基本上是我的主类的路径) - Manifest-Version: 1.0 Main-Class: .HelloWorld
  • @KetanDeopujari,看看这篇文章,“anexinet.com/blog/…

标签: java gradle jar build.gradle


【解决方案1】:

你可以试试下面的build.gradle,它会传递一个uber jar,它封装了所有的依赖(doc reference)。

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.3'
    }
}

apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'com.github.johnrengelman.shadow'

repositories {
    jcenter()
}

dependencies {
    compile 'com.google.guava:guava:23.0'

    testCompile 'junit:junit:4.12'
}

mainClassName = "com.github.Library"

dependencies{
    compile 'org.apache.poi:poi:3.17'
    compile 'org.apache.poi:poi-ooxml:3.17'
    compile 'org.apache.poi:poi-ooxml-schemas:3.17'
    compile 'org.springframework:spring-core:5.0.2.RELEASE'
    compile 'org.springframework:spring-context:5.0.2.RELEASE'
    compile 'org.apache.commons:commons-collections4:4.1'
    compile 'org.apache.xmlbeans:xmlbeans:2.6.0'
}

shadowJar {
    baseName = 'finder-app'
    version =  '0.1.0'
}

只需运行./gradlew :shadowJar,然后您就可以运行java -jar build/libs/finder-app-0.1.0-all.jar。这对你有利。

注意,您的buildscript{} 中不需要这么多不相关的依赖项,这些是供插件使用的。

【讨论】:

    【解决方案2】:

    要运行 java 应用程序,您需要提供类路径 你有两个选择:

    • 在 jar 清单 (META-INF/manifest) 中提供类路径,然后您可以使用

      运行您的 jar

      java -jar myjar.jar

    • 使用 -cp 参数提供类路径,然后您可以使用

    • 运行您的 jar

    java -cp list_of_dependencies_jar_and_main_jar package.MyMainClass

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-26
      • 1970-01-01
      • 2015-07-28
      • 2016-03-29
      • 1970-01-01
      • 2011-09-21
      • 2019-05-27
      • 1970-01-01
      相关资源
      最近更新 更多