Evaluation of java snippet failed for row "Row 0": org/dom4j/DocumentException

I try to run a piece of code in Java Snippet. It tries to list all the name of Sheets in an Excel File. I added all necessary jars in Addintional Libraries. And the code compiled successfully. But when i run the code it ruturn a ? in the output cell. I try to add some System.out.println into my codes to trace the procedure but it seems that the program have never reach my code and a WARNING message was print out in console window:

Evaluation of java snippet failed for row "Row 0": org/dom4j/DocumentException

Here is my code:

// Your custom imports:
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.util.ArrayList;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

// Enter your code here:
try {
    BufferedInputStream inp = new BufferedInputStream(
        new FileInputStream(c_Location));
    ArrayList<String> names = new ArrayList<String>();
    try {
      Workbook wb = WorkbookFactory.create(inp);
      for (int sIdx = 0; sIdx < wb.getNumberOfSheets(); sIdx++) {
           String name = wb.getSheetName(sIdx);
          names.add(name);
      }
    } finally {
      inp.close();
    }

    out_Sheets = names.toArray(new String[0]);
}
catch (Exception e) {
    out_Sheets = new String[] {"#Error: " + e.getMessage()};
}

 

What shall I do now? sad
 

The problem ist that the dom4j-1.6.1.jar file is missing, please add it to the additional libraries. You find it in the ooxml-lib folder of poi.

I have to hints to debug thes kind of issues. The exception is throw before your script is excuted. When you adjust the log level in your KNIME preferences you will see the exception in the KNIME console window.

Secondly, there is a way to log errors within your Java Snippe script. Yo can use logError("an error or exception") to utilize the KNIME logging system.

You are great.

I added all jar files and now it start running.
But when it run to the line:
Workbook wb = WorkbookFactory.create(inp);
A new exception was raised. It said that StyleSheetDocumentImpl cannot cast to StyleSheetDocument.

It is weird. The same piece of code I used in a standard concole program links to the same jars works fine.

Would you please give some hints why it happened?

Thank you very much.

Here are the stack tracing for the exception:

java.lang.reflect.InvocationTargetException

org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
    at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:61)
    at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:256)
    at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:196)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:172)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:63)
    at JSnippet.snippet(JSnippet.java:41)
    at org.knime.base.node.jsnippet.JavaSnippetCellFactory.getCells(JavaSnippetCellFactory.java:216)
    at org.knime.core.data.container.RearrangeColumnsTable.calcNewCellsForRow(RearrangeColumnsTable.java:507)
    at org.knime.core.data.container.RearrangeColumnsTable.calcNewColsSynchronously(RearrangeColumnsTable.java:421)
    at org.knime.core.data.container.RearrangeColumnsTable.create(RearrangeColumnsTable.java:346)
    at org.knime.core.node.ExecutionContext.createColumnRearrangeTable(ExecutionContext.java:372)
    at org.knime.base.node.jsnippet.JavaSnippet.execute(JavaSnippet.java:763)
    at org.knime.base.node.jsnippet.JavaSnippetNodeModel.execute(JavaSnippetNodeModel.java:133)
    at org.knime.core.node.NodeModel.execute(NodeModel.java:680)
    at org.knime.core.node.NodeModel.executeModel(NodeModel.java:536)
    at org.knime.core.node.Node.invokeNodeModelExecute(Node.java:995)
    at org.knime.core.node.Node.execute(Node.java:889)
    at org.knime.core.node.workflow.SingleNodeContainer.performExecuteNode(SingleNodeContainer.java:894)
    at org.knime.core.node.exec.LocalNodeExecutionJob.mainExecute(LocalNodeExecutionJob.java:100)
    at org.knime.core.node.workflow.NodeExecutionJob.run(NodeExecutionJob.java:166)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at org.knime.core.util.ThreadPool$MyFuture.run(ThreadPool.java:124)
    at org.knime.core.util.ThreadPool$Worker.run(ThreadPool.java:239)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:59)
    ... 24 more
Caused by: java.lang.ClassCastException: org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.StyleSheetDocumentImpl cannot be cast to org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument
    at org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument$Factory.parse(Unknown Source)
    at org.apache.poi.xssf.model.StylesTable.readFrom(StylesTable.java:102)
    at org.apache.poi.xssf.model.StylesTable.<init>(StylesTable.java:91)
    ... 29 more

 

Hello,

Just a vague guess from my side: do you have the XLS supporting nodes from KNIME installed? I guess they also have POI on classpath and the two implementations are from different classloaders so the Impl class do not implement the interface from the other classloader. (Can be other problem too.)

Cheers, gabor

Yes. I have XLS Reader and Writer in my KNIME installation.

In fact, these poi jars imported by me are exactly in KNIME/plugins/org.apache.poi_3.6.0.0034677

How to handler this situation? If i did not import these jars there will be a compile error and prevent me to execute the node.

:(

Thank you.

I'm afraid there is nothing you can do about this. XMLBeans (which are used by POI) have a strange way of using classloaders which interfers with the Eclipse classloaders.

sad. What a pity. So I have to find another way to do this job.

Thank you anyway.