线程“main”中的异常 java.lang.reflect.InvocationTargetException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45430888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:41:41  来源:igfitidea点击:

Exception in thread "main" java.lang.reflect.InvocationTargetException

javaitext

提问by hrishi

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"

我正在使用 iText PDF 5.5.11 将 PDF 转换为 XML。我已经在 stackoverflow 上检查了类似的答案。当我在 ubuntu 上使用命令行运行 jar 文件时出现以下错误。java版本“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.

我对java不太熟悉。我使用 PHP exec 函数从 PHP 调用这个 jar 文件。下面是我用来将 PDF 转换为 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();
    }
}

回答by Amedee Van Gasse

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.

当您将 Maven 用于您的 Java 项目时,您需要做的就是向 iText 添加一个依赖项。然后 Maven 将处理所有传递依赖项,如 BouncyCastle。Maven 带走了所有繁重的工作。同样的原则适用于其他构建系统,如 Gradle 等。

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.xmlof each and every of your dependencies, see which transitive dependencies they have, which dependencies those dependencies have, and so on ad nauseam.

现在,如果您想手动完成所有操作并将正确的 jars 放在类路径中,那么您需要做一些功课。这就是说,在pom.xml每一个你的依赖的,看看他们有哪些传递依赖,哪些依赖这些依赖有,等等令人作呕

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

对于 iText,您可以查看在pom.xmlMaven 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.

这告诉您 iText 5.5.11 对 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.

BouncyCastle 在随机更改和破坏 API 方面名声不佳,即使是很小的更新,这也是为什么您需要非常精确地使用 BouncyCastle 版本的原因。

回答by Manish Aman

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"

嗨,只需将 zookeeper.service 文件更改为 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”到下面,问题解决了:Environment="KAFKA_OPTS=-javaagent:/home/ec2-user/prometheus/jmx_prometheus_javaagent-0.3.1.jar=8080:/home/ec2-user/prometheus/zookeeper. yml"