【问题标题】:java.lang.NoClassDefFoundError: com/itextpdf/kernel/pdf/PdfWriter itext and gradlejava.lang.NoClassDefFoundError: com/itextpdf/kernel/pdf/PdfWriter itext 和 gradle
【发布时间】:2016-11-17 10:17:36
【问题描述】:

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

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

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

这是我的主要课程(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(' ')
    }
}

如您所见,我创建了一个任务来将所有依赖项 jar 复制到 builds/libs/lib 中

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

并设置罐子{ 依赖于 copyToLib }

但错误还是一样。

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

【问题讨论】:

    标签: java pdf gradle build.gradle itext7


    【解决方案1】:

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

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

    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 脚本。) .

    【讨论】:

      【解决方案2】:

      感谢您的帮助。使用应用程序插件是一个很好的解决方案!此外,我找到了另一种解决更改 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(' ')
          }
      }
      

      【讨论】:

      • 如果“应用程序”插件部分有帮助,您可以投票支持我的答案 ;-)
      • 好的。我刚刚给你投了票,但不幸的是我的声望点不到 15,所以我认为没有人能看到我的投票。
      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2019-12-01
        • 1970-01-01
        • 2017-06-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多