{"id":20,"date":"2003-12-17T18:08:51","date_gmt":"2003-12-17T18:08:51","guid":{"rendered":"http:\/\/www.pleus.net\/blog\/?p=20"},"modified":"2010-08-13T00:13:12","modified_gmt":"2010-08-12T22:13:12","slug":"vc-net-the-glue-to-integrate-net-and-java","status":"publish","type":"post","link":"https:\/\/www.pleus.net\/blog\/?p=20","title":{"rendered":"VC++.NET &#8211; The Glue To Integrate .NET and Java"},"content":{"rendered":"<p>Have you ever had the need for Java\/.NET Integration ?<br \/>\nAlthough the main approach is web services interop, sometimes it&#8217;s necessary to do some lower level integration.<br \/>\nFor example if you have a neat java library for instance for logging purposes which is tightly integrated in your application.<\/p>\n<p>Why not use it within your .NET application as well ? <\/p>\n<p>With VC++.NET and Java Native Interface (JNI) it&#8217;s not that complicated.<br \/>\nJust load the Java VM into the .NET process and call the Java methods with the help of unmanaged C++.<\/p>\n<p>Below you can see an example of how to call the method<br \/>\n<font face=\"courier\"><br \/>\n<b>boolean MyLogger.log(String name)<\/b><br \/>\n<\/font><\/p>\n<p>which resides in C:\\temp\\mylogger.jar and is part of the package net.pleus. The class uses log4j.<\/p>\n<p><font face=\"courier\"><br \/>\n#include &#8220;jni.h&#8221; \/\/ included in the jdk, link to jvm.lib as well<\/p>\n<p>\/\/ configure Java VM<br \/>\nJavaVMOption options[3];<br \/>\noptions[0].optionString = &#8220;-Djava.compiler=NONE&#8221;;<br \/>\noptions[1].optionString = &#8220;-Djava.class.path=C:\\\\temp\\\\mylogger.jar;C:\\\\temp\\\\log4j.jar&#8221;;<br \/>\noptions[2].optionString = &#8220;-verbose:jni&#8221;;<\/p>\n<p>JavaVMInitArgs vm_args;<\/p>\n<p>vm_args.version = JNI_VERSION_1_4;<br \/>\nvm_args.options = options;<br \/>\nvm_args.nOptions = 3;<br \/>\nvm_args.ignoreUnrecognized = JNI_FALSE;<\/p>\n<p>\/\/ load Java VM<br \/>\nJavaVM* jvm;<br \/>\nJNIEnv* env;<br \/>\nJNI_CreateJavaVM(&#038;jvm,(void **)&#038;env, &#038;vm_args);<\/p>\n<p>\/\/ find class and method<br \/>\njclass cls = env->FindClass(&#8220;net\/pleus\/MyLogger&#8221;);<br \/>\njmethodID mid = env->GetStaticMethodID(cls,&#8221;log&#8221;,&#8221;(Ljava\/lang\/String;)Z&#8221;);<\/p>\n<p>\/\/ call method<br \/>\njstring name = env->NewStringUTF(&#8220;my message&#8221;);<br \/>\nbool res = env->CallStaticObjectMethod(cls,mid,name);<\/p>\n<p>\/\/ cleanup<br \/>\njvm->DestroyJavaVM();<br \/>\n<\/font><\/p>\n<p>The technology stack could be as follows:<\/p>\n<p>Managed Code (C#, VB.NET) -> Hybrid C++.NET and JNI -> Java<\/p>\n<p>I tried it out. It works very well (and fast).<br \/>\nThe most difficult thing is to perform the type conversions and code the cumbersome signature descriptions.<br \/>\nThe other way round (call .NET from Java) is also possible though it&#8217;s a little bit trickier.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever had the need for Java\/.NET Integration ? Although the main approach is web services interop, sometimes it&#8217;s necessary to do some lower level integration. For example if you have a neat java library for instance for logging purposes which is tightly integrated in your application. Why not use it within your .NET &hellip; <a href=\"https:\/\/www.pleus.net\/blog\/?p=20\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">VC++.NET &#8211; The Glue To Integrate .NET and Java<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[22],"class_list":["post-20","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-c"],"_links":{"self":[{"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/20","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=20"}],"version-history":[{"count":2,"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/20\/revisions"}],"predecessor-version":[{"id":367,"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/20\/revisions\/367"}],"wp:attachment":[{"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=20"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=20"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pleus.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=20"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}