如何避免java.lang.NoSuchMethodError:org.apache.poi.util.IOUtils.copy(Ljava / IO / InputStream的; Ljava / IO / OutputStream的;)在Apache的POI [英] How to avoid java.lang.NoSuchMethodError: org.apache.poi.util.IOUtils.copy(Ljava/io/InputStream;Ljava/io/OutputStream;) in Apache POI

查看:3357
本文介绍了如何避免java.lang.NoSuchMethodError:org.apache.poi.util.IOUtils.copy(Ljava / IO / InputStream的; Ljava / IO / OutputStream的;)在Apache的POI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个code添加水印现有的.doc文件。

以下是code到目前为止,我已经试过

 公共静态无效的主要(字串[] args)
{    尝试
    {
        XWPFDocument = XDOC新XWPFDocument(新的FileInputStream(Test.doc的));
        XWPFHeaderFooterPolicy xFooter =新XWPFHeaderFooterPolicy(XDOC);
        xFooter.createWatermark(我的水印);
    }
    赶上(例外五){
        e.printStackTrace();
    }
}

以下是我得到了什么

 异常线程mainjava.lang.NoSuchMethodError:org.apache.poi.util.IOUtils.copy(Ljava / IO / InputStream的; Ljava / IO /的OutputStream;)V
在org.apache.poi.util.PackageHelper.open(PackageHelper.java:50)
在org.apache.poi.xwpf.usermodel.XWPFDocument<&初始化GT;(XWPFDocument.java:71)
在com.avi.Test.ReadDoc.main(Watermark.java:38)


解决方案

查看的Apache POI关于这个题目的FAQ条目。什么几乎肯定发生的事情是,你已经添加POI的新副本到你的classpath,但较旧的版本已经在那里了(从以前的需要,你的框架等)和Java现在越来越困惑要使用哪一个。

首先,你要使用code这样的片段制定出在哪里POI是来自:

 的ClassLoader类加载器=
   org.apache.poi.poifs.filesystem.POIFSFileSystem.class.getClassLoader();
URL解析度= ClassLoader.getResource方法,(
         组织/阿帕奇/ POI / POIFS /文件系统/ POIFSFileSystem.class);
字符串路径= res.getPath();
的System.out.println(+路核心POI是从哪里来的);

使用该识别旧罐子(S)和删除它们。

然后,使用 POI组件页面制定出什么罐子,你需要使用,以及它们的依赖关系。最后,添加最新的罐子到类路径中,你会好到哪里去!

I have a code for adding watermark to existing .doc file.

The following is the code I have tried so far

public static void main(String[] args)
{

    try
    {
        XWPFDocument xDoc = new XWPFDocument(new FileInputStream("test.doc"));
        XWPFHeaderFooterPolicy xFooter = new XWPFHeaderFooterPolicy(xDoc);
        xFooter.createWatermark("My Watermark");
    }
    catch(Exception e) {
        e.printStackTrace();
    }
}

The following is what I got

Exception in thread "main" java.lang.NoSuchMethodError: org.apache.poi.util.IOUtils.copy(Ljava/io/InputStream;Ljava/io/OutputStream;)V
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:50)
at org.apache.poi.xwpf.usermodel.XWPFDocument.<init>(XWPFDocument.java:71)
at com.avi.Test.ReadDoc.main(Watermark.java:38)

解决方案

See the Apache POI FAQ entry on this very topic. What has almost certainly happened is that you have added a new copy of POI to your classpath, but an older version was already there (from an earlier need, your framework etc), and Java is now getting confused about which one to use.

Firstly, you'll want to use a snippet of code like this to work out where POI is coming from:

ClassLoader classloader =
   org.apache.poi.poifs.filesystem.POIFSFileSystem.class.getClassLoader();
URL res = classloader.getResource(
         "org/apache/poi/poifs/filesystem/POIFSFileSystem.class");
String path = res.getPath();
System.out.println("Core POI came from " + path);

Use that to identify the older jar(s) and remove them.

Then, use the POI Components Page to work out what Jars you need to use, and what their dependencies are. Finally, add the latest jars to your classpath, and you'll be good to go!

这篇关于如何避免java.lang.NoSuchMethodError:org.apache.poi.util.IOUtils.copy(Ljava / IO / InputStream的; Ljava / IO / OutputStream的;)在Apache的POI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆