I'm using Gradle to setup a test project that uses itext 7 to generate pdf files.
If I run my main class in Netbeans IDE everything works fine; a "results" folder is created and inside it I can find the generated pdf.
But if I clean and build the project, go into project_folder/build/libs and try to execute java -jar mypdfproject.jar file i get this error => java.lang.NoClassDefFoundError: com/itextpdf/kernel/pdf/PdfWriter
this is my main class (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();
}
}
and this is the 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(' ')
}
}
as you can see I created a task to copy all the dependecies jars into builds/libs/lib
task copyToLib( type: Copy ) { into "$buildDir/libs/lib" from configurations.runtime }
and set jar{ dependsOn copyToLib }
but the error is still the same.
I think it should be a classpath error, but I don't know how and where to set the classpath in Gradle. How can I run my project from terminal?