Nick
Nick

Reputation: 480

java.lang.NoClassDefFoundError: com/itextpdf/kernel/counter/event/IMetaInfo

I'm new to Java, I'm working on generating pdf from html. Therefore, I'm using the iText7, I can generate a normal pdf file through PdfWriter and Document but I can't do it using html2pdf.

Here is my Pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>springexample</groupId>
  <artifactId>PDFGenerator</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>bean</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.itextpdf/itext7-core -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itext7-core</artifactId>
        <version>7.0.4</version>
        <type>pom</type>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.itextpdf/itext7-core -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>kernel</artifactId>
        <version>7.0.4</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>io</artifactId>
        <version>7.0.4</version>
    </dependency>

    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>layout</artifactId>
        <version>7.0.4</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>forms</artifactId>
        <version>7.0.4</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>pdfa</artifactId>
        <version>7.0.4</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>pdftest</artifactId>
        <version>7.0.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.itextpdf/html2pdf -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>html2pdf</artifactId>
        <version>2.1.4</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.26</version>
    </dependency>
</project>

and here is my code java:

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Paths;

import com.itextpdf.html2pdf.HtmlConverter;

public class HtmlToPdf {

/** The HTML-string that we are going to convert to PDF. */
public static final String HTML = "<h1>Test</h1><p>Hello World</p>";
/** The target folder for the result. */
public static final String TARGET = "target/";
/** The path to the resulting PDF file. */
public static final String DEST = String.format("%stest-01.pdf", TARGET);

        public static void main(String[] args) throws IOException { 

            HtmlConverter.convertToPdf(HTML, new FileOutputStream(DEST));

            System.out.println("Done");
        }
    }

The error is on the line:

HtmlConverter.convertToPdf(HTML, new FileOutputStream(DEST));

Exception in thread "main" java.lang.NoClassDefFoundError: com/itextpdf/kernel/counter/event/IMetaInfo
    at springexample.bean.HtmlToPdf.main(HtmlToPdf.java:18)
Caused by: java.lang.ClassNotFoundException: com.itextpdf.kernel.counter.event.IMetaInfo
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)

I read other issues related to

java.lang.NoClassDefFoundError

they said that I need to add the io and slf4j dependencies, however the error remains. Thanks in advance.

Upvotes: 4

Views: 8646

Answers (3)

Nandom Gusen
Nandom Gusen

Reputation: 61

With html2pdf, this error java.lang.NoClassDefFoundError: com/itextpdf/kernel/counter/event/IMetaInfo is resolved by upgrading your IText 7 Kernel to 7.2.5 as shown below

 <dependency>
     <groupId>com.itextpdf</groupId>
     <artifactId>kernel</artifactId>
     <version>7.2.5</version>
 </dependency>

Upvotes: 1

lukdymek
lukdymek

Reputation: 15

Yesterday I had simillar problem. It appears that some dependencies did not load properly even that no errors were thrown. I had to restart IDE (Eclipse in my case) and that sorted out the problem.

Upvotes: -1

e.g78
e.g78

Reputation: 697

com/itextpdf/kernel/counter/event/IMetaInfo is not present in version 7.0.4 Try upgrading to itext 7.1.7

Upvotes: 6

Related Questions