In my previous blog I have said something about using a java library in .Net . My case demanded me to use a java library in .Net . Many times we find a decent and clean library to use but its a java library and some time we find a java library that is impossible to ignore , in that case we hope for a way to use java api in .Net . I roamed the net and tried the fit and that fit happened to fit my case. That is IKVM.Net by Jeroen Frijters .
http://www.ikvm.net/index.html
IKVM.net is a open source java implementation , that comes with the OpenJdk implementation in .Net and various tools to interop between java and .Net . Whole JVM is implemented in .Net and its very easy to convert java jar files to .Net assemblies and .Net assemblies to java jars (interface and stub classes ).
Taking on my case to convert java jar files to .Net assemblies , there comes a command line utility with IKVM called ikvmc.exe . To generate the .Net assembly for a jar file we first specify the command line switch [-target:library] for generating a dll . Without this command line switch ikvmc.exe will generate a exe for a jar file if it encounters a main method in a class. On command prompt we change directory to IKVM binaries folder and specify command :
C:\IKVM>ikvmc.exe –target:library hapi-base-1.2.jar
The jar file is in same directory as ikvm binaries , this will generate the hapi-base-1.2.dll . There will be many warnings by ikvmc.exe of noclassdeffound , this is because we haven’t specified the dependency dll’s. IKVM comes with the OpenJdk implementation in .Net and they are in form of dll’s. Now we have to specify the OpenJdk dll’s that our jar file depends upon to rectify the warnings.
C:\IKVM>ikvmc.exe –target:library hapi-base-1.2.jar –reference:IKVM.OpenJDK.Core.dll -reference:IKVM.OpenJDK.Util.dll
In example I have only referenced some of OpenJdk dll’s , but we have to reference all the dll’s that our jar file depends upon. Upon completion of the command a hapi-base-1.2.dll will be generated . Now moving on to next jar file (hapi-structures-v21-1.2.jar).
C:\IKVM>ikvmc.exe –target:library hapi-structures-v21-1.2.jar –reference:IKVM.OpenJDK.Core.dll -reference:IKVM.OpenJDK.Util.dll
As this new jar file have dependency on previous jar file , we have to reference the dll of previous jar file to make a successful porting of this jar file to a dll.
C:\IKVM>ikvmc.exe –target:library hapi-structures-v21-1.2.jar –reference:hapi-base-1.2.dll –reference:IKVM.OpenJDK.Core.dll -reference:IKVM.OpenJDK.Util.dll
And so on we can create dll’s from jar files resolving the dependency using the [-reference:<dllname>] .Now these dll’s are ready to be used in your .Net project . We have to at least add reference of IKVM.Runtime.dll in our project and other OpenJdk dll’s that out converted jar dll’s depend upon. Keep on porting .
Hello Digvijay,
ReplyDeleteI am very interested in utilizing your solution to using hapi in a .net environment rather than the outdated nhapi library. If you have documented the information, would you be so kind as to post the full ordered series of ikvmc commands required to successfully compile all of hapi?
Regards,
Chris Whelan
Hi,
ReplyDeleteI am Parth. Even including all the dll's and jar references, while creating the dll it showed the classNotFound errors. After referencing and trying to implement a simple HAPI exmaple, it got all the classes and functions. But when creating a simple Parse object like Parser p = new GenericParser(); shows a error !!
Sir ,
ReplyDeleteI tried using hapi-base-2.1.jar and hapi-structures-v251-2.1.jar. Used Ikvm.
The parsing is also happening. Issue is when I parse the message, the result comes as a GenericMessage of type ca.uhn.hl7v2.model.GenericMessage.v251. but I dont know how to set value for ca.uhn.hl7v2.model.v251.message.ORU_R01 object.