【发布时间】: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