java.lang.NoSuchMethodError when trying to read .xlsm file

6.4k views Asked by At
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.

2

There are 2 answers

0
Selman Gun On

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>
0
m4gic On

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.