3

I am able to read XLS files properly in my OSGI based application, but when I try to read XLSX file, i get the following error.

Caused by: java.lang.ExceptionInInitializerError
    at org.apache.poi.openxml4j.opc.OPCPackage.init(OPCPackage.java:162)
    at org.apache.poi.openxml4j.opc.OPCPackage.<init>(OPCPackage.java:142)
    at org.apache.poi.openxml4j.opc.Package.<init>(Package.java:37)
    at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:128)
    at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:257)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:291)[242:export_poi.jar:0.0.0]
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:252)[242:export_poi.jar:0.0.0]
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:231)[242:export_poi.jar:0.0.0]
    at mycom.project2.ExcelAdapter.Sources.Source.readExcelFile(Source.java:67)[232:ExcelAdapter:1.0.0]
    at mycom.project2.ExcelAdapter.Sources.Source.<init>(Source.java:56)[232:ExcelAdapter:1.0.0]
    at mycom.project2.ExcelAdapter.Sources.SourcesManager.addSource(SourcesManager.java:55)[232:ExcelAdapter:1.0.0]
    at mycom.project2.ExcelAdapter.Sources.SourcesManager.addSources(SourcesManager.java:48)[232:ExcelAdapter:1.0.0]
    at mycom.project2.ExcelAdapter.Processors.Engine.setResourceConfig(Engine.java:44)[232:ExcelAdapter:1.0.0]
    at mycom.project2.ExcelAdapter.ExcelAdapter.configureBusinessLogic(ExcelAdapter.java:164)[232:ExcelAdapter:1.0.0]
    at mycom.project1.function_engine_tooling.fb.libraries.FunctionBlockType.addFunctionBlockInstance(FunctionBlockType.java:161)[234:fb-libraries:1.0.0]
    at mycom.project1.function_engine_tooling.fb.libraries.FunctionBlockType.handleCreateFbInstances(FunctionBlockType.java:373)[234:fb-libraries:1.0.0]
    at mycom.project1.function_engine_tooling.fb.libraries.FunctionBlockType.onMessageReceived(FunctionBlockType.java:197)[234:fb-libraries:1.0.0]
    at mycom.project2.ExcelAdapter.ExcelAdapterInstanceFactory.onMessageReceived(ExcelAdapterInstanceFactory.java:46)[232:ExcelAdapter:1.0.0]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.8.0_131]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)[:1.8.0_131]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.8.0_131]
    at java.lang.reflect.Method.invoke(Method.java:498)[:1.8.0_131]
    at org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:408)
    at org.apache.camel.component.bean.MethodInfo$1.doProceed(MethodInfo.java:279)
    at org.apache.camel.component.bean.MethodInfo$1.proceed(MethodInfo.java:252)
    ... 19 more
Caused by: java.lang.ClassCastException: org.apache.xerces.stax.XMLEventFactoryImpl cannot be cast to javax.xml.stream.XMLEventFactory

I am using this command to read the files:

WorkbookFactory.create(new File("fileName"));  

I have tried various solutions online, which tell me to exclude certain artifactIDs e.g: stax-api and stax. But none of the solutions seem to be working.

CoderX
  • 942
  • 1
  • 10
  • 30

2 Answers2

3

The problem seems to be that there are two bundles that export the package javax.xml.stream. It seems that org.apache.xerces.stax.XMLEventFactoryImpl is wired to one of these packages and your bundle to another. So they do not see the same classes even if they are named the same.

To avoid this make sure only one bundle exports this package. One way to achieve this is to export the package from the system bundle as it should be present in the jre.

At least for apache karaf the installation is simple. Use these bundles:

install -s mvn:commons-codec/commons-codec/1.10
install -s mvn:org.apache.commons/commons-collections4/4.1
install -s mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.poi/3.16_1
Christian Schneider
  • 19,420
  • 2
  • 39
  • 64
  • 1
    On close inspection I found out that CLASS `javax.xml.stream.XMLEventFactory` is being provided by other BUNDLE **poiDependencies.jar** (which must be actually used, since I have exported all relevant POI dependencies in to a separate JAR) as well as by the OSGI lib (via **xerces-2.11.0.jar**). According to your asnwer, the error is that instead of using CLASS from **poiDependencies.jar**, ServiceMix is using it from **xerces-2.11.0.jar**?? I tried deleting the CLASS from **xerces-2.11.0.jar**, but it is internally needed by ServiceMix (as it casued an error). How can I approach such issue? – CoderX Aug 05 '17 at 10:11
  • Did you try the bundles from my answer? – Christian Schneider Aug 07 '17 at 07:44
  • **commons-codec** is already installed, I installed the other two bundles but it gives the same error. – CoderX Aug 11 '17 at 08:11
  • Did you remove your poiDependencies bundle? – Christian Schneider Aug 11 '17 at 08:32
0

I open XLSX files with this code with no issue:

    InputStream inp = new FileInputStream(inputfilename);
    Workbook wb = WorkbookFactory.create(inp);
    // Open the specified sheet
    Sheet inputsheet = wb.getSheetAt(sheetindex);

I don't know if an inputstream is necessary, but it definitely does the job.

Tim S.
  • 2,187
  • 1
  • 25
  • 40
  • I have used this as well, but it gives me the same error. :( – CoderX Aug 01 '17 at 18:59
  • 1
    Well it appears you're mis-casting a variable: `ClassCastException: org.apache.xerces.stax.XMLEventFactoryImpl cannot be cast to javax.xml.stream.XMLEventFactory` What line is throwing this error? The one you posted? – Tim S. Aug 01 '17 at 19:11
  • Yes. I have also updated the question with more explanation of the error. – CoderX Aug 01 '17 at 19:43
  • Sorry its difficult for me to provide more code. But on close inspection I found out that CLASS `javax.xml.stream.XMLEventFactory` is being provided my other BUNDLE **poiDependencies.jar** (which must be actually used, since I have exported all relevant POI dependencies in to a separate JAR) as well as by the OSGI lib (via **xerces-2.11.0.jar**). Could this be the error that instead of using CLASS from **poiDependencies.jar**, ServiceMix is using it from **xerces-2.11.0.jar**?? I tried deleting **xerces-2.11.0.jar**, but it is internally needed by ServiceMix. How can I approach such issue? – CoderX Aug 05 '17 at 09:55
  • Remove the javax.xml.stream from your posDependencies.jar .. and check if you have other conflicts in there. – Christian Schneider Aug 05 '17 at 10:15