我正在使用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中设置类路径。我如何从终端运行我的项目?
如果“应用”部分插件帮助了,你可以投票我的回答了;-) – gaganbm
确定。我刚刚投票支持你,但不幸的是我得到的声望低于15点,所以我认为没有人能看到我的投票。 – Mavek