17

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)
Avinash Mishra
  • 1,346
  • 3
  • 21
  • 41
  • 2
    Check the version of your jars, you are mixing different versions. Googling for `org.apache.poi.util.IOUtils` will tell you which jar contains it, and at which version a `copy(InputStream, OutputStream)` method was introduced. – SJuan76 Aug 14 '13 at 12:02

8 Answers8

34

I got this error today: "java.lang.NoSuchMethodError:org.apache.poi.util.POILogger.log(I[Ljava/lang/Object;)V]"

It looks different from your error, but quite similar. FYI, I'm using maven to manage jars. After some experiment, I found out the root case is the poi.jar and poi-ooxml.jar's version are not consistent.

This configuration will get an error:

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.12</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.13</version>
    </dependency>

I changed the version of poi.jar from 3.12 to 3.13

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.13</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.13</version>
    </dependency>

bingo, problem solved. I hope this will help someone who ran into this kind of Exception.

sofia
  • 763
  • 8
  • 11
  • The [Apache POI FAQ is pretty clear](http://poi.apache.org/faq.html#faq-N1019C) that mixing POI jars between versions is not supported, so this is largely to be expected! – Gagravarr Mar 02 '16 at 10:20
  • I did not mix up the versions but an update from 3.10.1 to 3.13 (consistently) worked for me anyway. Where 3.15 (currently the latest stable) caused other errors. – Simon Mar 07 '17 at 11:59
  • Thanks, you saved my day. – Bin Jun 07 '17 at 07:46
  • Just to complete the answer with another tip, I had the same problem with POI version 3.16 and after seeing your answer I changed everything to 3.15 and everything start working. – jfajunior Aug 21 '17 at 07:54
  • for 3.16 it does not work, so I will use 3.13 (which seems to work). Because apache POI does not know how to play with maven :-) – Leo Mar 17 '18 at 11:40
8

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!

Gagravarr
  • 47,320
  • 10
  • 111
  • 156
  • I have `poi-ooxml-schemas-3.7.jar` `poi-ooxml-3.7.jar`, `xbean.jar`. Now getting : `Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/util/POILogFactory at org.apache.poi.POIXMLDocumentPart.(POIXMLDocumentPart.java:40) at com.avi.Test.ReadDoc.main(ReadDoc.java:46) Caused by: java.lang.ClassNotFoundException: org.apache.poi.util.POILogFactory at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source)` – Avinash Mishra Aug 14 '13 at 13:13
  • 1
    Have another look at the [components page](http://poi.apache.org/overview.html#components), which clearly states that poi-ooxml depends on poi, so you need to add that jar in too. Also, 3.7 is old, consider a newer version! – Gagravarr Aug 14 '13 at 13:21
  • Also, look for mismatches. For me i was bringing in poi at version 5.0.0 and poi-ooxml at 5.2.2. Bringing poi to 5.2.2 as well solved my issue. – spetz83 Jul 20 '22 at 15:18
4

You almost certainly have an older version of POI on your classpath.

See The Apache POI FAQ

M. Abbas
  • 6,409
  • 4
  • 33
  • 46
1

Go here: http://poi.apache.org/download.html

download the tar.gz -> extract it and add to the build classPath all the jars from it.

Xelian
  • 16,680
  • 25
  • 99
  • 152
1

align the pom versions of 'poi' and 'poi-ooxml' it will do the work

<dependencies>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.17</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.17</version>
    </dependency>
</dependencies>
Ziv.Ti
  • 609
  • 7
  • 10
1

I had a similar issue with apache Tika (java.lang.NoSuchMethodError Error): javax.servlet.ServletException: java.lang.NoSuchMethodError: org.apache.poi.util.IOUtils.closeQuietly(Ljava/io/Closeable;)

I resolved the issue by updating the POI jars.

user26316
  • 39
  • 2
0

I got same problems like you,the solution is you need to import all jar files to run your program.These are mandatory for running your project

  • Poi-3.10-Final.jar
  • Poi-ooxml-3.10-Final.jar
  • Poi-ooxml-schemas-3.10.jar
  • Xmlbeans-2.30.jar
estmatic
  • 3,449
  • 1
  • 19
  • 28
venkyreddy
  • 21
  • 2
0

From the very beginning, poi-ooxml and poi version must be identical.

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.16</version> <---------------this.
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.16</version> <---------------this.
</dependency>
Tiina
  • 4,285
  • 7
  • 44
  • 73