我正在使用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(类型:复制){ 进入“$ buildDir / libs / lib” 来自configurations.runtime }
并设置jar { 依赖copyToLib }
但错误仍然相同。
我认为它应该是一个类路径错误,但我不知道如何以及在何处设置Gradle中的类路径。如何从终端运行我的项目?
答案 0 :(得分: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(' ')
}
}
答案 1 :(得分: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脚本) )。
答案 2 :(得分:0)
我从https://jar-download.com/download-handling.php位置下载并添加到Build Path内核7.1.4 jar和styled-xml-parser.jar及其所有依赖项中,从而消除了错误。 注意:如果在支持脱机maven的环境中工作,则可以通过从项目的pom.xml文件所在的位置运行mvn dependency:go-offline命令来解决此问题。