Chemaxon .NET API¶
This manual gives you a walk-through on how to use the Chemaxon .NET API.
Table of Contents¶
- Introduction
- Notes for JChem .NET API users
- What's changed?
- Licensing
- New assemblies
- Marvin Editor Control
- About the zip file
- Multi-threading
- Callbacks
- OLE handling
- Tips and tricks for JChem .NET API users
Introduction¶
All of the chemical knowledge of Chemaxon is written in Java. To make it available in .NET applications, a third party tool, called IKVM was used till the Iodine LTS release (September, 2021). IKVM can translate Java bytecode to .NET Intermediate Language (.NET IL) but it supports 8 or lower versions of Java only. In October 2021, Chemaxon switched to Java 11 so a new solution had to be implemented for the .NET -> Java interaction. The Java Native Interface (JNI) technology was selected for this task in the new Chemaxon .NET API.
Notes for JChem .NET API users¶
Please note that the reconstruction of the .NET API focused on Chemaxon .NET API. Although several classes are available from JChem .NET, not all methods in all of these classes are implemented. As a long term plan, we would like to provide all Java classes in JChem .NET that is used by our clients (to avoid major changes in the integration codes), but at the moment, only a selected subset is supported. Please contact us if you miss a method in your integration code or even add your own wrapper implementation to your project. For help in doing so see the Tips and tricks for JChem .NET API users section.
What's changed?¶
The wrapper classes are the basis of the new JNI-based solution. Wrapper classes have to be implemented for all the used Java classes to make those callable in .NET code. Under the frame of the first version of the New Chemaxon .NET API, the focus was on the classes that are used in JChem for Office and were available in the old Chemaxon .NET API. These classes are wrapped, but a lot of other classes in JChem .NET API (related to JChem, Marvin, Calculator Plugins, etc. products) are still missing. As a future plan, we would like to provide all of the wrapper classes of the JChem .NET API used by our clients.
With 24.3.0 we introduced some further changes in the Chemaxon.NET interfaces, as follows:
-
Signature change in the constructors of the java.lang.Object class and its descendant classes, that is most wrapper classes in Chemaxon.NET. The type of javaObject parameter changed from System.IntPtr to java.JNI.JNIObjRef.
-
One consequence of the above is that if you added new classes (directly or indirectly) inheriting from java.lang.Object, you’ll also need to modify the signature of your classes accordingly. Simply replacing System.IntPtr to java.JNI.JNIObjRef as the type of the javaObject parameter of your constructor(s) should resolve this.
- The java.JNI.JavaNativeInterface class no longer implements IJavaNativeInterface, actually we removed this interface completely.
- One consequence of the above change is that it’s no longer necessary to explicitly attach / detach to a JNIEnv if you want to use Chemaxon.NET classes from multiple threads. By simply using the JNIEnv property of the JavaNativeInterface class, the correct association between a JNIEnv and its corresponding thread is implicitly ensured.
Licensing¶
The new Chemaxon .NET API requires the same ChemAxon .NET API license as before.
New assemblies¶
There are 3 new DLLs in the JNI-based Chemaxon .NET API:
Java.JNI.dll
ChemAxon.JNI.dll
ChemAxon.Com.JNI.dll
These DLLs are responsible for the communication to the Java Virtual Machine (JVM) and these assemblies contain the wrapper classes for the Chemaxon .NET API.
The original 4 DLLs of the Chemaxon .NET API are also available and required to be referred in a .NET application:
ChemAxon.NET.Base.dll
ChemAxon.IKVM.dll
ChemAxon.Windows.Forms.dll
ChemAxon.NET.dll
Marvin Editor Control¶
MarvinEditorControl and MarvinSkecthForm do not work in the JNI-based Chemaxon .NET API. The new similar implementation is the MarvinSketchJFrame form. The following example illustrates the initialization and the usage of it. Please note this form is not modal.
About the zip file¶
The structure of the zip file¶
The jre folder contains the Java runtime environments. There are a 32-bit (jre32.zip) and a 64-bit (jre64.zip) version of it.
Chemaxon's chemical knowledge written in Java is in the lib folder as jar files.
The bin folder contains the Chemaxon .NET API DLLs and its third party references for the custom .NET applications. The main assemblies that have to be referred in a .NET project are the follows:
- ChemAxon.NET.Base.dll
- ChemAxon.NET.IKVM.dll
- ChemAxon.NET.Windows.Forms.dll
- ChemAxon.NET.dll
- Java.JNI.dll
- ChemAxon.JNI.dll
- ChemAxon.Com.JNI.dll
Setup for the Java Virtual Machine (JVM)¶
Unzip the zip packages in the jre folder then rename the jre32 subfolder to "x86" and the jre64 to "x64". Both have to have jvm.dll in it:
1 2 | |
About the Java.JNI.dll.config file¶
If the jre or the lib folder has a different location relative to the bin folder then these directory paths have to be stored in the Java.JNI.dll.config file.
For example:
1 2 | |
The initial and the maximum memory allocation pool for the Java Virtual Machine (JVM) can be set in the Java.JNI.dll.config file.
1 2 | |
By default these values mean megabytes [m/M] but kilobytes [k/K] or gigabytes [g/G] also can be used. For example:
1 2 | |
Multi-threading¶
When implementing multithreaded applications using the new, JNI-based Chemaxon .NET API, an asynchronous code block that is expected to run on different thread(s) must be passed to the method JavaNativeInterface.ExecuteAsAsync as an Action/Function.
Examples of usage¶
As the first step, the entry point to Java Native Interface is to be obtained by calling JavaNativeInterfaceFactory.GetJavaNativeInterface.
The method ExecuteAsAsync must be used for all code blocks that are expected to run on different threads (note that the thread can also be the “main” thread, which was used upon the creation of JavaNativeInterface):
Callbacks¶
Callbacks aren’t implemented yet. It is not possible to subscribe to Listener interfaces, nor to PropertyChangeListeners.
MarvinSketchJFrame.ShowAsDialog method polls the state of the displayed Marvin editor control to be notified when the window is closed for obtaining the edited molecule. This is handled transparently.
OLE handling¶
Marvin OLE Server has been removed from Chemaxon .NET API. It will not be available in the next JNI-based JChem for Office releases either. Thus, copying, pasting or editing Marvin OLE objects will not be available in the future.
IOleHandler interface and its implementation has been removed, and is not available in the New Chemaxon .NET API.
Tips and tricks for JChem .NET API users¶
Missing wrapper classes or methods can be added manually to a .NET application for extending the JChem .NET API. Furthermore, all of the wrapped Java functions are virtual in the JChem .NET API so these can be overwritten easily.
References:
For standard Java classes
For JChem classes
Important note: added classes have to have the same namespaces as the original classes have. For example the QueryBond classes are missing in the new .NET API. It can be added to a custom .NET application but it has to be under the namespace chemaxon.struc. So the full qualified name of the class has to be chemaxon.struc.QueryBond.
Example of usage
Extending a class:
Extending a custom class:
Adding a missing constructor:
Adding a copy constructor:
Implementing a void function:
Implementing a non-void function:
Implementing a static function:
Overwriting a non-virtual function:
The example classes:
The whole example project is available in the Chemaxon .NET API zip package in the Examples\JChemApiExtension subfolder.
Please visit the following links for more information: