首页 文章

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

提问于
浏览
1

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

如果我在Netbeans IDE中运行我的主类,一切正常;创建“results”文件夹,在其中我可以找到生成的pdf .

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

这是我的主类(MyPdfMain.class)

package com.mypackage;

import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import java.io.File;
import java.io.IOException;

public class MyPdfMain {

    public static final String DEST = "results/pdf/hello_word.pdf";

    /**
     * @param args the command line arguments
     * @throws java.io.IOException
     */
    public static void main(String[] args) throws IOException {

        File file = new File(DEST);
        file.getParentFile().mkdirs();


        //Initialize PDF writer
        PdfWriter writer = new PdfWriter(DEST);

        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(writer);

        // Initialize document
        Document document = new Document(pdf);

        //Add paragraph to the document
        document.add(new Paragraph("Hello World!"));

        //Close document
        document.close();
    }
}

这是build.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 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(' ')
    }
}

正如您所看到的,我创建了一个任务,将所有dependecies jar复制到builds / libs / lib中

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

并设置jar

但错误仍然是一样的 .

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

3 回答

  • 0

    谢谢您的帮助 . 使用应用程序插件是一个很好的解决方案!除此之外我找到了另一种解决方法来改变我的build.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(' ')
        }
    }
    
  • 0

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

    另一种方法是,你可以在build.gradle中应用'application'插件:

    group 'Hello-World'
    version '1.0-SNAPSHOT'
    
    apply plugin: 'java'
    apply plugin: 'application'
    
    jar{
        manifest {
            attributes 'Main-Class': 'com.mypackage.MyPdfMain'
        }
    }
    

    然后你可以做 gradle build 哪个应该创建目录 build/distribution

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

  • 1

    我下载并添加到Build Path kernel-7.1.4 jar和styled-xml-parser.jar以及来自https://jar-download.com/download-handling.php位置的所有依赖项,这样就消除了错误 . 注意:如果在启用脱机maven的环境中工作,则可以通过从项目的pom.xml文件所在的位置运行mvn dependency:go-offline命令来解决此问题 .

相关问题