1

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?

Amedee Van Gasse
  • 7,280
  • 5
  • 55
  • 101
Mavek
  • 63
  • 1
  • 6

3 Answers3

1

Thank you for your help. Using the application plugin is a good solution! Besides I found another way to solve changing my 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(' ')
    }
}
Mavek
  • 63
  • 1
  • 6
  • If the 'application' plugin part helped out, you may vote my answer up ;-) – gaganbm Jul 15 '16 at 08:02
  • Ok. I've just voted you up, but unfortunately I've got less than 15 reputation points so I think no one can see my vote. – Mavek Jul 15 '16 at 16:46
0

You are copying the dependent jars to lib directory, and creating your application jar. You need to define that in your classpath in command line. Refer this

Another way could be, you can apply 'application' plugin in your build.gradle :

group 'Hello-World'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'application'

jar{
    manifest {
        attributes 'Main-Class': 'com.mypackage.MyPdfMain'
    }
}

Then you can do gradle build which should create directories build/distribution

You will find your application zipped inside that directory, which you can just unzip and execute the shell file from the bin directory (which you will see after unzipping. The unzipped directory will have a shell script inside bin directory.).

Community
  • 1
  • 1
gaganbm
  • 2,663
  • 3
  • 24
  • 36
0

I downloaded and added to the Build Path kernel-7.1.4 jar and styled-xml-parser.jar and all its dependencies from the location https://jar-download.com/download-handling.php and that did away with the error. Note: if working in an offline maven enabled environment this problem can be addressed through running mvn dependency:go-offline command from the location wherein the pom.xml file of the project is located.

raikumardipak
  • 1,461
  • 2
  • 29
  • 49