2

I have a problem integrating Java code into KNIME. Similar posts on Knime forum (http://tech.knime.org/forum/knime-general/using-external-jar-in-java-snippet-node-workflow-not-able-to-initialize-class-of) were of little help and I also posted a question there but have not got answer so far, so I'm trying my luck here.

I am trying to integrate my code into KNIME workflow using JavaSnippet. I have exported the code into a jar and put it into the KNIME jre/lib/endorsed folder. The code references CDK 1.4.19 and I have also placed the corresponding jar file into the same directory. I do not have CDK node extensions installed in KNIME and using them is also not an option in my case.

The code starts with:

IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();

SmilesParser sp= new SmilesParser(builder);

When I try to execute JavaSnippet I get the following exception message:

Evaluation of java snippet failed for row "Row0". Exception message: Could not initialize class org.openscience.cdk.smiles.SmilesParser

When I just try

IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();

It works and I get no exception message. I have checked, the builder is not a null. However, when I try to initialize SmilesParser, it fails. This class is public. It has no default constructor and has one public constructor that takes IChemObjectBuilder as a parameter.

I have tried to use class loader:

URL[] classLoaderUrls = new URL[]{new URL("file:///path on my computer/knime_2.9.4/jre/lib/endorsed/cdk-1.4.19.jar")};

URLClassLoader urlClassLoader = new URLClassLoader(classLoaderUrls);

Class<?> parserClass = urlClassLoader.loadClass("org.openscience.cdk.smiles.SmilesParser");

Class[] classParameters = new Class[] {IChemObjectBuilder.class};

Constructor<?> constructor = parserClass.getConstructor(classParameters); //until this line there are no problems

Object parser = constructor.newInstance(builder);   //fails here with the same exception message: Could not initialize class org.openscience.cdk.smiles.SmilesParser

I am sure that this is not a CDK error because I can execute the code in Eclipse.

Why can a constructor of one class be called from KNIME without any problems and a constructor of another class can not??

I would be very grateful if you could suggest a solution or a probable reason why this happens.

Thank you!

T3KBAU5
  • 1,861
  • 20
  • 26
  • Have you tried updating to the latest KNIME version 2.11.3? Are you sure that your JAR file cdk-1.4.19.jar contains SmilesParser class and all its dependencies (CDK is notoriously difficult to bundle properly with all its dependencies)? Have you tried updating to the latest CDK version 1.5.X? – user1808924 May 11 '15 at 14:51
  • 1
    My point is that error message "Could not initialize class org.openscience.cdk.smiles.SmilesParser" has nothing to do with constructors and such. Most probably, class SmilesParser tries to do some work in its static initializers, but fails because of missing dependencies (or smth like that). It's most probably a JAR packaging and/or class loading issue. – user1808924 May 11 '15 at 14:54
  • Thank you for your reply. I've updated KNIME and it did not help. I have used the same JAR and added it as a dependency in java project in Eclipse. It works perfectly fine there, so actually all the dependencies required for SmilesParser are also packed up there. The problems start when I try to integrate the code along with all its dependencies into KNIME workflow. Any other ideas? – Мария Рябошапкина May 12 '15 at 09:13

1 Answers1

1

Ok, finally solved the problem myself. Cleaned project meta data, cleaned endorsed library directory, switched KNIME workspace to another directory, put all jars into one folder and added them as external libraries. Now it works:)