尝试在doc文件中复制图片时出现错误

时间:2019-01-02 20:56:14

标签: java selenium

尝试通过硒在do文件中复制图片时出现错误消息。

这是我遇到的错误-

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException
    at LeadFreeTest.docCapture.main(docCapture.java:17)
Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlException
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

下面是代码

    package LeadFreeTest;
    import java.io.*;
    import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
    import org.apache.poi.xwpf.usermodel.*;
    import org.apache.poi.xwpf.usermodel.XWPFDocument;
    import org.apache.poi.xwpf.usermodel.XWPFParagraph;
    import org.apache.poi.xwpf.usermodel.XWPFRun;

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    public class docCapture {


            @SuppressWarnings("resource")
            public static void main(String[] args) throws IOException, InvalidFormatException 
            {
                XWPFDocument docx = new XWPFDocument();
                XWPFParagraph par = docx.createParagraph();
                XWPFRun run = par.createRun();
                run.setText("Hello, World. This is my first java generated docx-file. Have fun.");
                run.setFontSize(13);
                InputStream pic = new FileInputStream("C:\\Naveeen\\TestScreenShot\\LoginPage.png");
                //byte [] picbytes = IOUtils.toByteArray(pic);
                //run.addPicture(picbytes, Document.PICTURE_TYPE_JPEG);
                run.addPicture(pic, Document.PICTURE_TYPE_JPEG, "3", 0, 0);
                FileOutputStream out = new FileOutputStream("C:\\Naveeen\\TestScreenShot\\LoginPage.doc"); 
                docx.write(out); 
                out.close(); 
                pic.close();
            }
        }

1 个答案:

答案 0 :(得分:0)

您需要将XML bean依赖项添加到类路径中,以使

ClassNotFoundException: org.apache.xmlbeans.XmlException

该库通常称为xmlbeans-x.x.x.jar

您可以找到它here