java.lang.NoClassDefFoundError:com/itextpdf/kernel/pdf/PdfWriter itext和gradle

java.lang.NoClassDefFoundError:com/itextpdf/kernel/pdf/PdfWriter itext和gradle,java,pdf,gradle,build.gradle,itext7,Java,Pdf,Gradle,Build.gradle,Itext7,我正在使用Gradle设置一个测试项目,该项目使用itext 7生成pdf文件 如果我在Netbeans IDE中运行我的主类,一切都很好;创建了一个“结果”文件夹,在其中我可以找到生成的pdf 但是如果我清理并构建项目,进入project_文件夹/build/libs并尝试执行java-jar mypdfproject.jar文件,我会得到这个错误=>java.lang.NoClassDefFoundError:com/itextpdf/kernel/pdf/PdfWriter 这是我的主类(

我正在使用Gradle设置一个测试项目,该项目使用itext 7生成pdf文件

如果我在Netbeans IDE中运行我的主类,一切都很好;创建了一个“结果”文件夹,在其中我可以找到生成的pdf

但是如果我清理并构建项目,进入project_文件夹/build/libs并尝试执行java-jar mypdfproject.jar文件,我会得到这个错误=>java.lang.NoClassDefFoundError:com/itextpdf/kernel/pdf/PdfWriter

这是我的主类(MyPdfMain.class)

这就是这个建筑,格雷德尔

apply plugin: 'java'

sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

if (!hasProperty('mainClass')) {
    ext.mainClass = 'com.mypackage.MyPdfMain'
}

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'com.itextpdf', name: 'kernel', version: '7.0.0'
    compile group: 'com.itextpdf', name: 'io', version: '7.0.0'
    compile group: 'com.itextpdf', name: 'layout', version: '7.0.0'
    compile group: 'com.itextpdf', name: 'forms', version: '7.0.0'
    compile group: 'com.itextpdf', name: 'pdfa', version: '7.0.0'
    compile group: 'com.itextpdf', name: 'pdftest', version: '7.0.0'
    testCompile group: 'junit', name: 'junit', version: '4.10'
}

task copyToLib( type: Copy ) {
    into "$buildDir/libs/lib"
    from configurations.runtime
}

jar{
    dependsOn copyToLib
    manifest {
        attributes 'Main-Class': 'com.mypackage.MyPdfMain'
        //        attributes 'Class-Path': configurations.compile.collect { it.getName() }.join(' ')
    }
}
如您所见,我创建了一个任务,将所有依赖项JAR复制到builds/libs/lib中

任务copyToLib(类型:Copy){ 进入“$buildDir/libs/lib” 从configurations.runtime }

把罐子放好{ 德彭森copyToLib }

但是错误仍然是一样的


我认为这应该是一个类路径错误,但我不知道如何在Gradle中以及在哪里设置类路径。如何从终端运行项目?

您正在将依赖jar复制到lib目录,并创建应用程序jar。 您需要在命令行的类路径中定义它。提及

另一种方法是,您可以在build.gradle中应用“应用程序”插件:

group 'Hello-World'
version '1.0-SNAPSHOT'

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

jar{
    manifest {
        attributes 'Main-Class': 'com.mypackage.MyPdfMain'
    }
}
然后您可以执行
gradlebuild
,它应该创建目录
build/distribution


您将在该目录中找到压缩的应用程序,您只需从
bin
目录中解压并执行shell文件(解压后您将看到该目录。解压后的目录将在bin目录中有一个shell脚本。)。

感谢您的帮助。使用应用程序插件是一个很好的解决方案!此外,我还找到了另一种方法来解决更改构建的问题。gradle:

apply plugin: 'java'

sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

if (!hasProperty('mainClass')) {
    ext.mainClass = 'com.mypackage.MyPdfMain'
}

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'com.itextpdf', name: 'kernel', version: '7.0.0'
    compile group: 'com.itextpdf', name: 'io', version: '7.0.0'
    compile group: 'com.itextpdf', name: 'layout', version: '7.0.0'
    compile group: 'com.itextpdf', name: 'forms', version: '7.0.0'
    compile group: 'com.itextpdf', name: 'pdfa', version: '7.0.0'
    compile group: 'com.itextpdf', name: 'pdftest', version: '7.0.0'
    testCompile group: 'junit', name: 'junit', version: '4.10'
}

task copyDependenciesIntoBuildLibsDir( type: Copy ) {
    from configurations.runtime
    into "$buildDir/libs/lib"
}

jar{ dependsOn copyDependenciesIntoBuildLibsDir
    manifest {
        attributes 'Main-Class': 'com.mypackage.MyPdfMain'
        attributes 'Class-Path': configurations.runtime.collect { "lib/" + it.getName()}.join(' ')
    }
}

我从该位置下载并添加到构建路径kernel-7.1.4jar和styled-xml-parser.jar及其所有依赖项,这样就消除了错误。
注意:如果在支持maven的脱机环境中工作,可以通过从项目的pom.xml文件所在的位置运行命令来解决此问题。

如果“应用程序”插件部分有帮助,您可以投票支持我的答案;-)好啊我刚刚投票支持你,但不幸的是,我的声望不足15分,所以我想没人能看到我的投票。
apply plugin: 'java'

sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

if (!hasProperty('mainClass')) {
    ext.mainClass = 'com.mypackage.MyPdfMain'
}

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'com.itextpdf', name: 'kernel', version: '7.0.0'
    compile group: 'com.itextpdf', name: 'io', version: '7.0.0'
    compile group: 'com.itextpdf', name: 'layout', version: '7.0.0'
    compile group: 'com.itextpdf', name: 'forms', version: '7.0.0'
    compile group: 'com.itextpdf', name: 'pdfa', version: '7.0.0'
    compile group: 'com.itextpdf', name: 'pdftest', version: '7.0.0'
    testCompile group: 'junit', name: 'junit', version: '4.10'
}

task copyDependenciesIntoBuildLibsDir( type: Copy ) {
    from configurations.runtime
    into "$buildDir/libs/lib"
}

jar{ dependsOn copyDependenciesIntoBuildLibsDir
    manifest {
        attributes 'Main-Class': 'com.mypackage.MyPdfMain'
        attributes 'Class-Path': configurations.runtime.collect { "lib/" + it.getName()}.join(' ')
    }
}