当我 运行 此代码时出现错误

i get an error when i run this code

package demo;

import java.io.File; 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;

import org.apache.poi.openxml4j.opc.*; 
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class DocxToPdf {
    public static void main(String[] args){
    try
        {
            String inputFile = "F:\MY WORK\CollectionPractice\WebContent\APCR1.docx";
            String outputFile = "F:\MY WORK\CollectionPractice\WebContent\APCR1.pdf";

            System.out.println("inputFile:" + inputFile + ",outputFile:" + outputFile);

            FileInputStream in = new FileInputStream(inputFile);

            XWPFDocument document = new XWPFDocument(in);

            File outFile = new File(outputFile);

            OutputStream out = new FileOutputStream(outFile);

            PdfOptions options = null;

            PdfConverter.getInstance().convert(document, out, options);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

当我 运行 这段代码出现这样的错误时,我也使用了以下 jar 文件。

error: java.lang.NoSuchMethodError: org.apache.poi.POIXMLDocumentPart.getPackageRelationship()Lorg/apache/poi/openxml4j/opc/PackageRelationship;

罐子: List of jar files

您可能混淆了 POI 的 jar 版本。该错误表明加载的 class 没有调用 class 在编译期间看到的方法,因此您的 class 路径中有不同版本的 POI。

请参阅 https://poi.apache.org/overview.html 中的 "Component Map" 以了解包含的不同组件以及它们最终包含哪些 jar,确保您的 class 路径中只有这些 jar 之一,而不是多个不同的版本。