Using Java Classes in a .NET project

Let me guess,Witchcraft ?

No witchcraft was involved (directly anyway, I did sacrifice a lamb to get some blue mountain coffee to keep myself awake when looking through alternative solutions), neither is mangling JAVA source code into C# form.

But .NET has everything!!!!

Java has a rich ecosystem with many reusable projects in existence for it which is great if you are working in a Java environment but if you need to develop a program using Microsoft .NET technologies and wish to leverage a Java class then you may be SOL (sadly outta luck).

I was in this situation a while ago when integrating some JAVA-only semantic web tools (Pellet and JENA) into a .NET web project, the solution I came to was to use an implementation of JAVA for Mono/.NET called IKVM.

IKVM allows cross-compilation of Java JAR files to .NET compatible DLL files which can be then be imported into your .NET project

This conversion process looks similar to

image

Lets go!

Download the iKVM toolset and extract it to a memorable location (e.g. c:\ikvm) and then compile or download a .JAR file for the Java classes that you wish to use.

Once you have obtained your .JAR file you need to compile this to the .DLL to do this put the .JAR into the same folder as your ikvmc.exe file (above this extracted this to c:\ikvm\bin ) and then perform the conversion.

The command to run the conversation may vary if your JAVA component has dependencies, to read more about this go to the IKVM documentation.

A simple conversion of the Jena.JAR files

   1:  ikvmc .\jena\*.jar -target:library -out:.\dotNet-Libs\jena.dll -version:2.5.4.0 -nowarn:IKVMC0109

This operation will be performed on the following Jena related .JAR files

imageThis produces the following output the warnings refer to dependencies that Jena requires that have not been satisfied in this cross compile, these will be compiled to DLL files in another process.

image

This will produce a single jena.dll file in the dotNet-Libs folder, this DLL file will need to be imported into the .NET project in addition to all of the standard  IKVM.OPENJDK and IKVM reflection and runtime DLL files (these provide a runtime).

To compile the entire set of Pellet 1.5.2, MSSQL JDBC driver, Jena 2.5.2 JAR  files (with dependencies) it may be best to run a batch file with the following series of operations (assuming you have the correct jar files in the listed folders)

   1:  mkdir dotNet-Libs 
   2:  mkdir pellet 
   3:  mkdir sqldriver 
   4:  move aterm-java-1.6.jar .\pellet 
   5:  move pellet-core.jar .\pellet 
   6:  move pellet-jena.jar .\pellet 
   7:  move servlet.jar .\pellet 
   8:  move sqljdbc.jar .\sqldriver 
   9:  rem Convert Pellet 1.5.2 auxiliary libraries 
  10:  ikvmc .\xsdlib\*.jar -target:library -out:.\dotNet-Libs\xsdlib.dll -version:1.5.2.0 -nowarn:IKVMC0109 
  11:  ikvmc .\jetty\*.jar -target:library -out:.\dotNet-Libs\jetty.dll -version:1.5.2.0 -nowarn:IKVMC0109 
  12:  ikvmc .\junit\*.jar -target:library -out:.\dotNet-Libs\junit.dll -version:1.5.2.0 -nowarn:IKVMC0109 
  13:  ikvmc .\owlapi\*.jar -target:library -out:.\dotNet-Libs\owlapi.dll -version:1.5.2.0 -nowarn:IKVMC0109 
  14:  rem Convert Jena 2.5.4 
  15:  ikvmc .\jena\*.jar -target:library -out:.\dotNet-Libs\jena.dll -version:2.5.4.0 -nowarn:IKVMC0109 
  16:  rem Convert Pellet 1.5.2 libraries 
  17:  ikvmc .\pellet\*.jar -target:library -reference:.\dotNet-Libs\xs dlib.dll -reference:.\dotNet-Libs\jetty.dll -reference:.\dotNet-Libs\junit.dll -reference:.\dotNet-Libs\owlapi.dll -reference:.\dotNet-Libs\jena.dll -out:.\dotNet-Libs\pellet.dll -version:1.5.2.0 -nowarn:IKVMC0109 
  18:  rem Convert Microsoft jdbc SQL Driver 1.2 
  19:  ikvmc .\sqldriver\*.jar -target:library -out:.\dotNet-Libs\sqldriver.dll -version:1.2.0.0 -nowarn:IKVMC0109 
  20:  copy IKVM.*.dll .\dotNet-Libs\ 
  21:  pause

Once you have generated all the Java origin DLL files you will need to import these into your .NET project in the usual way (if you don’t know how to do this i suggest that you trawl MSDN for a few hours to learn all the basics of .NET development).

Bumps in the road?

Yes, you need to be wary of Namespace conflicts, e.g. if both .NET  and Java have an available object called shinyObject your will need to explicitly call the FULL name of this object in your code as shown when creating the JENA statement and GenericRuleReasoner objects in the following code snippet

   1:  using System;
   2:  using System.Data;
   3:  using System.Configuration;
   4:  using System.Linq;
   5:  using System.Web;
   6:  using System.Web.Security;
   7:  using System.Web.UI;
   8:  using System.Web.UI.HtmlControls;
   9:  using System.Web.UI.WebControls;
  10:  using System.Web.UI.WebControls.WebParts;
  11:  using System.Xml.Linq;
  12:  using System.IO;
  13:  using SpeechLib;
  14:  using System.Media;
  15:   
  16:  using System.Collections;
  17:   
  18:  //Java Functionality
  19:  using ikvm.io;
  20:  using java.io;
  21:  using ikvm.lang;
  22:   
  23:  //Import Pellet
  24:  using org.mindswap.pellet;
  25:  using org.mindswap.pellet.jena;
  26:   
  27:  //IMPORT JENA TOOLS
  28:  using com.hp.hpl;
  29:  using com.hp.hpl.jena;
  30:  using com.hp.hpl.jena.db.impl;
  31:  using com.hp.hpl.jena.ontology;
  32:  using com.hp.hpl.jena.rdf.model;
  33:  using com.hp.hpl.jena.reasoner;
  34:  using com.hp.hpl.jena.query;
  35:  using com.hp.hpl.jena.util.iterator;
  36:   
  37:  //JENA TDB
  38:  using com.hp.hpl.jena.tdb;
  39:  using com.hp.hpl.jena.tdb.store;
  40:  using com.hp.hpl.jena.tdb.@base.file;
  41:   
  42:   
  43:  /* 
  44:   * 
  45:   * 'Statement' is an ambiguous reference between 'SemWeb.Statement' and 'com.hp.hpl.jena.rdf.model.Statement'
  46:   * 
  47:   */
  48:   
  49:   
  50:  public class sourMash
  51:  {
  52:   
  53:    
  54:      //A Jena Model RDF Statement
  55:      private static com.hp.hpl.jena.rdf.model.Statement jenaStatement = null;
  56:   
  57:        //Generate a Rule Reasoner
  58:        com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner reasoner = null;
  59:   
  60:  }

 

I hope this helps you in the beginning of your search for harmony between your development tools.

For convenience I have attached all the required JAR files, DLL Files, tools and scripts (ik-com.bat) required to compile and import PELLET and JENA in .NET here:  Pellet and Jena dotNET to Java Archive (this archive requires the excellent open source 7-ZIP file manager)


Comments

  1. i love your blog, i have it in my rss reader and always like new things coming up from it.

  2. Perfect ideas! I have been seeking for some thing such as this for a time today. With thanks!

  3. Book-marked, I love your site! :)

  4. Exactly where is the facebook like button ?

  5. This is exactly something I need to find more information about, appreciate the publish.

  6. Thanks very much! i think you saved my life:) i have the same problem as you had, .Net semantic web project and need to use a reasoner, and there is no .net reasoner:( or do you know about any?:)

  7. Hi Rene/oasisfan,

    Im glad I helped you :) Knowledge is for sharing.

    There is an old .NET reasoner called SemWEB but its not that capable and is no longer developed. The Jena/Pellet solution detailed above seems to be one of the best for Java at the moment.

    I may be able to post a comparison of Semantic Reasoners that I produced for my MSC degree, I will first need to check if this would be permitted by the University.

    There is a really interesting reasoner called Hermit that seems to have a high success rate for reasoning and great performance so I may look into building this for .NET (http://hermit-reasoner.com/).

    I hope your degree goes well, if there is anything else I can help you with feel free to ask @ https://plus.google.com/109777051033159650766

    Joe

Leave a Reply

Your email address will not be published / Required fields are marked *

9oveKo

Please type the text above: