4
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.xmlbeans.XmlOptions.setSaveAggressiveNamespaces()Lorg/apache/xmlbeans/XmlOptions;
at org.apache.poi.POIXMLDocumentPart.<clinit>(POIXMLDocumentPart.java:56)
at rulebooksToExcel.GenerateExcel.generateExcel(GenerateExcel.java:34)
at rulebooksToExcel.ParseNortDocFiles.main(ParseNortDocFiles.java:165)

I am getting the error at :

workbook = new XSSFWorkbook(in);

I read other similar questions but they all suggest XMLBeans Version 2.0+. But I am using 2.6, and I can't find any other explanation for what might be causing this.

Johnny000
  • 2,058
  • 5
  • 30
  • 59
DT7
  • 1,615
  • 14
  • 26
  • 2
    possible duplicate of [How to solve a NoSuchMethodError when using POI for doc files](http://stackoverflow.com/questions/11423861/how-to-solve-a-nosuchmethoderror-when-using-poi-for-doc-files) – akokskis Aug 29 '13 at 15:31
  • 2
    Looks like you're running with a very old copy of xmlbeans... Can you try upgrading to a supported version (2.3+) and trying again? – Gagravarr Aug 29 '13 at 20:00
  • @Gagravarr, as I mentioned in the question, I am using XMLBeans 2.6. – DT7 Aug 30 '13 at 15:06
  • @akokskis It's not a duplicate.. I have seen thru several questions and almost all of them suggest problem with XMLBeans versions. i have 2.6 version but I still get the error. I have set the classpath and path accordingly. – DT7 Aug 30 '13 at 16:08
  • 2
    @BeginnerJava Per the [POI FAQ](http://poi.apache.org/faq.html#faq-N10006), have you verified that you are in fact using the correct versions of POI jars? – akokskis Aug 30 '13 at 16:20
  • 2
    Are you sure you're really using the version of XMLBeans you think you are? Only if you put 1.x and 2.x on the classpath, you'll think you're on 2.x, but most likely you'll still run with 1.x! – Gagravarr Aug 30 '13 at 19:49

2 Answers2

0

I bumped into the same error in Java8.

This accepted answer helped me out:

I added the following lines to my code:

ClassLoader classloader = XmlOptions.class.getClassLoader();
        URL res = classloader.getResource(
                 "org/apache/xmlbeans/XmlOptions.class");
        String path = res.getPath();
        LOGGER.debug("XmlOptions.class loaded from " + path);

It turned out that if you use Java8 JRE to run your application, you got this in the log: XmlOptions.class loaded from file:/C:/Program%20Files/Java/jre1.8.0_131/lib/ext/xbean.jar!/org/apache/xmlbeans/XmlOptions.class

So in Java8, there is a apache xmlbeans jar in the JRE/lib/ext that will be loaded first regardless of your classpath settings!

When I changed the JAVA_HOME/PATH to point to my installed JDK8, the problem solved.

After a short check this is true for JRE7 too. The problem is that the JRE-bundled xbean.jar contains XmlOptions::setSaveAggresiveNamespaces (with one 's') in both cases.

(My used POI version was 3.17 because this version is compatible with Java7.)

Update: it turned out that this was true in my corporate env (prepared machine/group policy), at home on my PC there is no such jar.

m4gic
  • 1,461
  • 12
  • 19
0

You may have an older version of xmlbeans on your classpath, in an application server environment (As m4gic said).

I run that code snippet and saw that XmlOptions was being loaded from Weblogic modules;

XmlOptions.class loaded from file:/home/bea/bea12214/wlserver/modules/com.bea.core.xml.xmlbeans.jar!/org/apache/xmlbeans/XmlOptions.class

I solved this problem by forcing Weblogic to use application packages for xmlbeans. (For war type deployments it is in the weblogic.xml under WEB-INF folder)

 <wls:prefer-application-packages>
     ...
     <wls:package-name>org.apache.xmlbeans.*</wls:package-name>
 </wls:prefer-application-packages>
Selman Gun
  • 141
  • 1
  • 7