2016-07-14 29 views
0

我正在使用Gradle来设置一个使用itext 7生成pdf文件的测试项目。java.lang.NoClassDefFoundError:com/itextpdf/kernel/pdf/PdfWriter itext和gradle

如果我在Netbeans IDE中运行我的主类,一切正常;创建一个“结果”文件夹,并在里面我可以找到生成的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(' ') 
    } 
} 

,你可以看到我创建了一个任务复制所有德pendecies罐子到构建/库/ lib目录

任务copyToLib(类型:复制){ 为 “$ buildDir /库/ lib目录” 从configurations.runtime }

,并设置罐子{ dependsOn copyToLib }

但是错误仍然是一样的。

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

回答

0

您正在将依赖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脚本)。

1

谢谢你的帮助。使用应用程序插件是一个很好的解决方案!此外,我发现了另一种方式来解决改变我的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

如果“应用”部分插件帮助了,你可以投票我的回答了;-) – gaganbm

+0

确定。我刚刚投票支持你,但不幸的是我得到的声望低于15点,所以我认为没有人能看到我的投票。 – Mavek