{"id":2268,"date":"2011-01-20T11:27:19","date_gmt":"2011-01-20T09:27:19","guid":{"rendered":"https:\/\/test.viaboxx.de\/2011\/01\/20\/java-interoperation-with-a-net-dll-using-jni4net\/"},"modified":"2021-08-11T10:17:59","modified_gmt":"2021-08-11T10:17:59","slug":"java-interoperation-with-a-net-dll-using-jni4net","status":"publish","type":"post","link":"https:\/\/www.viaboxx.de\/en\/blog\/java-interoperation-with-a-net-dll-using-jni4net\/","title":{"rendered":"Java interoperation with a .NET DLL using jni4net"},"content":{"rendered":"\n<p>Imagine, that your requirement is to use a DLL, that contains .NET components under Windows. Your first thought could be <a href=\"http:\/\/en.wikipedia.org\/wiki\/Java_Native_Interface\">JNI<\/a>, but calling .NET components requires much more glue code, than just calling the functions that the DLL exports. In our projects, we have made good experiences with a framework, that uses reflection to generate proxy classes for seamless interoperation between java and .NET.<\/p>\n\n\n\n<p>This is what I want to demonstrate in this blog post. The framework is jni4net \u2013 a bridge between java and .NET on Microsoft Windows platforms. It&#8217;s an open-source framework with an LGPL runtime license.<\/p>\n\n\n\n<p>We just wanted to call .NET components from our Java application. It is also possible to launch a JVM and call Java from a .NET application, but this is not demonstrated in this post.<\/p>\n\n\n\n<p>Here is our example model, the names are just arbitrary examples for a project structure with its dependencies:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>jModel.jar: <\/strong>some Java interfaces and models<\/li><li><strong>netLogic.DLL:<\/strong> .NET Code (here: Visual Basic.NET, but the same works with C# or other .NET languages as well) that use the models, implement the interfaces from jModel.jar<\/li><li><strong>jApplication.jar:<\/strong> A Java application using the classes from both netLogic.DLL and jModel.jar<\/li><\/ul>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Example Model<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"http:\/\/www.viaboxx.de\/wp-content\/uploads\/2014\/06\/example-jni4net1.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"499\" height=\"393\" src=\"http:\/\/www.viaboxx.de\/wp-content\/uploads\/2014\/06\/example-jni4net1.jpg\" alt=\"example-jni4net1\" class=\"wp-image-2269\" srcset=\"https:\/\/www.viaboxx.de\/wp-content\/uploads\/2014\/06\/example-jni4net1.jpg 499w, https:\/\/www.viaboxx.de\/wp-content\/uploads\/2014\/06\/example-jni4net1-300x236.jpg 300w\" sizes=\"(max-width: 499px) 100vw, 499px\" \/><\/a><\/figure>\n\n\n\n<p><strong>All you need <\/strong>is an installation of .NET (comes with Visual Studio or the Windows SDK for Server) and the jni4net framework, and, of course, a JDK for compiling the Java parts. All running under Windows.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>netLogic.dll references: jni4net.n.w32.v20-0.8.2.0.dll, jni4net.n-0.8.2.0.dll and jModel.j4n.dll<br>\nYou can create a Visual Studio project for the .NET code of netLogic with those references.<\/li><li>jApplication.jar referenes: jModel.jar, jModel.j4n.jar and netLogic.j4n.jar<br>\nYou can use Apache Ant or Maven to build it. In this example, we use Ant, because the generated artifacts of the jni4net framework can used more easily with ant, while you should upload them in a maven repository when using maven.<\/li><\/ul>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Build process<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Compile and create jModel.jar. Nothing special here. Just plain java. You can use ant or maven.<\/li><li>Call the jni4net tool \u201cproxygen.exe\u201d to create jModel.j4n.jar and jModel.j4n.dll for interoperation with .NET.<br>\nThe tool \u201cproxygen.exe\u201d also creates a shell script \u201cbuild.cmd\u201d to compile the generated code to build the .jar and .dll.<\/li><li>Call msbuild.exe or use the Visual Studio IDE to build the .NET project, that uses the Java classes from jModel.jar.<\/li><li>Call jni4net tool \u201cproxygen.exe\u201d to create netLogic.j4n.jar and netLogic.j4n.dll for interoperation with Java.<\/li><li>Compile and create jApplication.jar, you can use ant or maven. Don\u2019t forget the dependencies to jModel and netLogic.j4n.dll and the jni4net.jar. (jni4net.jar\/.dll are runtime components that you also need to deploy with your application)<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted notranslate\"># step 2\nproxygen jModel.jar -wd target1\ntarget1build.cmd\n\n# step 3\nmsbuild.exe netLogic.sln \/t:Rebuild \/p:Configuration=Release\n\n# step 4\nproxygen.exe netLogic\/obj\/Release\/netLogic.dll\n&nbsp;-wd target2 -dp target1jModel.j4n.dll -cp target1jModel.j4n.jar;target1jModel.jar\ntarget2build.cmd<\/pre>\n\n\n\n<p class=\"has-normal-font-size\"><strong>Bootstrap: Java calling VB.NET<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Java:\nBridge.init();\nBridge.LoadAndRegisterAssemblyFrom(new File(\"jModel.j4n.dll\"));\nBridge.LoadAndRegisterAssemblyFrom(new File(\"netLogic.j4n.dll\"));<\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Data Type Conversion between Java and .NET<\/strong><\/p>\n\n\n\n<p>The bridge automatically converts most data types to their counterpart.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Java:\n\npublic interface ExampleModule {\n  String[] getTypes();\n}\n\nVB.NET:\nPublic Class MyVBExampleModule\n&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Implements ExampleModule\n  Public Function getTypes() As java.lang.String() Implements ExampleModule.getTypes\n    ...\n  End Function\nEnd Class<\/pre>\n\n\n\n<p>For more information, refer to the <a href=\"http:\/\/jni4net.sourceforge.net\/\">jni4net homepage<\/a>. Although the framework claims to have \u201calpha quality\u201d, there are no problems when using it in some of our products.<\/p>\n\n\n\n<p>We hope that the barrier to leave the Java world, through these examples, is diminished!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Imagine, that your requirement is to use a DLL, that contains .NET components under Windows. Your first thought could be JNI, but calling .NET components requires [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"default","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[76],"tags":[],"class_list":["post-2268","post","type-post","status-publish","format-standard","hentry","category-code"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Simon Tiffert","author_link":"https:\/\/www.viaboxx.de\/en\/blog\/author\/simon-tiffertviaboxx-de\/"},"uagb_comment_info":0,"uagb_excerpt":"Imagine, that your requirement is to use a DLL, that contains .NET components under Windows. Your first thought could be JNI, but calling .NET components requires [&hellip;]","_links":{"self":[{"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/posts\/2268"}],"collection":[{"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/comments?post=2268"}],"version-history":[{"count":3,"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/posts\/2268\/revisions"}],"predecessor-version":[{"id":7069,"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/posts\/2268\/revisions\/7069"}],"wp:attachment":[{"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/media?parent=2268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/categories?post=2268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.viaboxx.de\/en\/wp-json\/wp\/v2\/tags?post=2268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}