Have you ever found a neat-looking API that would save you tons of time and pain, only to have your hopes crushed when you discover that the API is written in Java?  Well, fret no more, because there’s a nice, easy way to leverage tasty Java APIs from within .NET: just compile them to IL using IKVM.NET!

I’m currently using the excellent Weka machine learning library from .NET.  Here’s the code:

   1: /// <summary>
   2: /// Simple ad-hoc class for testing out the Weka API from .NET.
   3: /// </summary>
   4: public class AdHocTests
   5: {
   6:     /// <summary>
   7:     /// Tests COBWEB.
   8:     /// </summary>
   9:     public void CobwebTest()
  10:     {
  11:         string input = @"D:\Program Files (x86)\Weka-3-5\data\soybean.arff";
  13:         Instances instances = new Instances(new java.io.FileReader(input));
  15:         Cobweb cobweb = new Cobweb();
  17:         Console.WriteLine("Clusters before: {0}.", cobweb.numberOfClusters());
  19:         cobweb.buildClusterer(instances);
  21:         Console.WriteLine("Clusters after: {0}.", cobweb.numberOfClusters());
  23:         Console.WriteLine(cobweb.graph());
  24:     }
  25: }

To convert an existing Java .jar file into a .NET DLL, simple run “ikvmc.exe –target:library something.jar”.  After a bit of chugging, you should get “something.dll”, ready for use in .NET.  Be sure you add references to IKVM.OpenJDK.ClassLibrary.dll and IKVM.Runtime wherever you use your new DLL.