2016-07-14 33 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