I'm trying to use a Java library called CDK with Mathematica's J/Link.

ReinstallJava[ClassPath -> $jar];
sp = JavaNew["org.openscience.cdk.smiles.SmilesParser", 
vabcVolume = JavaNew["org.openscience.cdk.geometry.volume.VABCVolume"];
atom = sp@parseSmiles["C"]

This short code is the Java equivalent to:

SmilesParser   sp  = new
IAtomContainer m   = sp.parseSmiles("C");

As you can see, I need to convert the SmilesParser object to IAtomContainer object. In Java, the compiler will do it for me. But I guess Mathematica can't do it automatically. Can someone tell me how I can get an IAtomContainer object (this is an Interface object, not class object) in Mathematica?


1 Answer 1


I had this same confusion when first interacting with java (using the exact same library) - all the functions I want to use take an IAtomContainer as their input, but I can only seem to make AtomContainer objects.

The answers to this question, "Can we instantiate an interface in Java 8?" are relevant here. Particularly,

The quick answer would be “No”. you can never instantiate a interface in java however, you can refer to an object that implements an interface by the type of the interface.

For instance, here is a static method that takes a molecular formula string and returns an IAtomContainer:

ac = MolecularFormulaManipulator`getAtomContainer[

(* «JavaObject[org.openscience.cdk.AtomContainer]» *)

The method actually returns an AtomContainer: a concrete class that implements the interface IAtomContainer.

Just to be clear - the molecule generated from the formula string is just a collection of atoms, with no bond information:

In[12]:= ac@getBondCount[]

Out[12]= 0

I can't speak to why the VABCVolume descriptor is complaining, but it isn't due to feeding it the wrong type of argument:

sp = JavaNew["org.openscience.cdk.smiles.SmilesParser", getInstance[]];
atom = sp @ parseSmiles @ "C";
LoadJavaClass @ "org.openscience.cdk.geometry.volume.VABCVolume";
VABCVolume`calculate @ atom 

During evaluation of In[23]:= Java::excptn: A Java exception occurred: org.openscience.cdk.exception.NoSuchAtomTypeException: The AtomType null could not be found
    at org.openscience.cdk.config.AtomTypeFactory.getAtomType(AtomTypeFactory.java:268)
    at org.openscience.cdk.geometry.volume.VABCVolume.calculate(VABCVolume.java:96).

Out[24]= $Failed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.