3

I am using iText PDF 5.5.11 to convert PDF to XML.I already checked similar answers on stackoverflow. I am getting below error when I run jar file using command line on ubuntu. java version "1.8.0_101"

 Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.NoClassDefFoundError: org/bouncycastle/asn1/ASN1Encodable
    at com.itextpdf.text.pdf.PdfEncryption.<init>(PdfEncryption.java:147)
    at com.itextpdf.text.pdf.PdfReader.readDecryptedDocObj(PdfReader.java:1063)
    at com.itextpdf.text.pdf.PdfReader.readDocObj(PdfReader.java:1469)
    at com.itextpdf.text.pdf.PdfReader.readPdf(PdfReader.java:751)
    at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:198)
    at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:236)
    at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:224)
    at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:214)
    at test.pdfreader.readXml(pdfreader.java:34)
    at test.pdfreader.main(pdfreader.java:30)

I am not much familiar with java. I call this jar file from PHP using PHP exec function. Below is the code I use to convert PDF to XML.

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.XfaForm;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class pdfreader {
    public static void main(String[] args) throws IOException, DocumentException, TransformerException {
        String SRC = "";
        String DEST = "";

        for (String s : args) {
            SRC = args[0];
            DEST = args[1];
        }
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new pdfreader().readXml(SRC, DEST);
    }

    public void readXml(String src, String dest) throws IOException, DocumentException, TransformerException {
        PdfReader reader = new PdfReader(src);

        AcroFields form = reader.getAcroFields();
        XfaForm xfa = form.getXfa();
        Node node = xfa.getDatasetsNode();
        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); ++i) {
            if ("data".equals(list.item(i).getLocalName())) {
                node = list.item(i);

                break;
            }
        }
        list = node.getChildNodes();

        Transformer tf = TransformerFactory.newInstance().newTransformer();
        tf.setOutputProperty("encoding", "UTF-8");
        tf.setOutputProperty("indent", "yes");
        FileOutputStream os = new FileOutputStream(dest);

        tf.transform(new DOMSource(node), new StreamResult(os));
        reader.close();
    }
}
5
  • 5
    Well it looks like you don't have BouncyCastle in your classpath, basically... Commented Aug 1, 2017 at 6:54
  • 1
    Agreed aboved, Caused by: java.lang.NoClassDefFoundError: org/bouncycastle/asn1/ASN1Encodable, add it to your build path or deployment directory if you are deploying it. Commented Aug 1, 2017 at 6:57
  • 1
    ok. It means I have to add jar file for BouncyCastle ? I can not find it in downloaded iText jar list Commented Aug 1, 2017 at 6:58
  • 1
    I added bouncycastle.jar in build path but same error Commented Aug 1, 2017 at 7:05
  • 2
    bouncycastle jars have compatibility issues when randomly used with iText. Please check whether the version of the bouncycastle jar that you have added is compatible with the iText version being used. Commented Aug 1, 2017 at 7:17

2 Answers 2

4

When you use Maven for your Java project, then all you need to do, is add a dependency to iText. Maven will then take care of all transitive dependencies like BouncyCastle. Maven takes away all the heavy lifting. The same principle applies for other build systems like Gradle etc.

Now, if you want to do it all manually and put the correct jars on your classpath, then you need to do some homework. This means looking at the pom.xml of each and every of your dependencies, see which transitive dependencies they have, which dependencies those dependencies have, and so on ad nauseam.

In case of iText, you take a look at the pom.xml that you can find on Maven Central: https://search.maven.org/#artifactdetails%7Ccom.itextpdf%7Citextpdf%7C5.5.11%7Cjar

In particular this part:

  <dependencies>
    <dependency>
      <groupId>org.bouncycastle</groupId>
      <artifactId>bcprov-jdk15on</artifactId>
      <version>1.49</version>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.bouncycastle</groupId>
      <artifactId>bcpkix-jdk15on</artifactId>
      <version>1.49</version>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.santuario</groupId>
      <artifactId>xmlsec</artifactId>
      <version>1.5.1</version>
      <optional>true</optional>
    </dependency>
  </dependencies>

This tells you that iText 5.5.11 has an optional dependency on BouncyCastle 1.49.

BouncyCastle has a bad reputation of randomly changing and breaking their API even with minor updates, that is why you need to be very precise with your BouncyCastle version.

Sign up to request clarification or add additional context in comments.

7 Comments

Exception in thread "main" java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58) Caused by: java.lang.NullPointerException at maven_1.pdfreader.readXml(pdfreader.java:40) at maven_1.pdfreader.main(pdfreader.java:30) ... 5 more
Edit your question to add more information. You are not giving me enough information to understand your problem.
I added all dependencies. code is working but for few PDF getting error as Exception in thread "main" java.lang.NullPointerException at maven_1.pdfreader.readXml(pdfreader.java:39) at maven_1.pdfreader.main(pdfreader.java:30)
line 39 is NodeList list = node.getChildNodes();
Does it mean PDF is not XFA ?
|
0

Hi Just change in zookeeper.service file as Environment="KAFKA_ARGS=-javaagent:/home/ec2-user/prometheus/jmx_prometheus_javaagent-0.3.1.jar=8080:/home/ec2-user/prometheus/kafka-0-8-2.yml" to below and the issue resolved: Environment="KAFKA_OPTS=-javaagent:/home/ec2-user/prometheus/jmx_prometheus_javaagent-0.3.1.jar=8080:/home/ec2-user/prometheus/zookeeper.yml"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.