• Home
  • About
Blue Orange Green Pink Purple

Using Java libraries in a C# app with IKVM

Posted in Uncategorized. on Wednesday, December 16th, 2009 by Chris Hulbert
Dec 16

I'm sure all us C# guys, at some point or another, have looked 'over the fence' with envy at all those juicy Java libraries. In my case, recently I needed to export and import some Excel files, and found that there aren't really any good C# libraries for it.

So i'd heard of IKVM and thought, why not give it a try? And it worked, really simply I might add! So here are the steps. Note that they're pretty generic across most libraries, but in my case this is all relating to the JXL library.

Firstly download IKVM. I grabbed the ikvmbin-0.40.0.1.zip file.
Then grab your JAR file from the library you want to use. Copy it into the ikvm\bin folder, and from the command prompt, do this:
ikvmc.exe my-jar-file.jar -target:library
This will (hopefully) produce a my-jar-file.dll file. This is your .Net compiled version of the library.

To use this new library, you'll want to copy it into your project and add it as a reference to your .net application, and do the same with some of the dll's from the IKVM\bin folder. I needed the following dll's for JXL, but you may need more or less, depending on what subset of the java standard library your library uses:

IKVM.OpenJDK.Core.dll
IKVM.OpenJDK.Text.dll
IKVM.OpenJDK.Util.dll
IKVM.Runtime.dll

Then, in my code, you can call the library from C#, just as if it was a .Net library. See the example for JXL below:

using System;
using jxl;
using jxl.write;

namespace JxlFromCSharp
{
  class Program
  {
    static void Main(string[] args)
    {
      WritableWorkbook book =
        Workbook.createWorkbook(new java.io.File("new.xls"));
      WritableSheet sheet = book.createSheet("First Sheet", 0);
      Label lbl = new Label(0, 2, "My Label");
      sheet.addCell(lbl);
      book.write();
      book.close();
    }
  }
}

As an aside, i've since found a really good native .Net excel library (also it's open source): http://npoi.codeplex.com/

1 Comment

  1. Matthew Jones on January 15th, 2010

    Hi Chris,
    I preference Apache POI.
    You should continue to look over your .Net fence.

    All the best.
    Matt Jones.



Leave a Reply

Chris' Babble

  • About
    Hi, i'm Chris Hulbert, a software guy from Sydney Australia.
  • Categories
    • code
    • Portfolio
    • Uncategorized
  • Recent Articles
    • How to implement DES and Triple DES from scratch
    • How to use Cookies in Struts 2 with ServletRequest and ServletResponse
    • How to use sessions with Struts 2
    • Using Quartz Scheduler in a Java web app (servlet)
    • Javascript date picker that Doesn't Suck!(tm)
    • Using Oracle XE with Hibernate


  • Home
  • About

© Copyright Chris' Babble. All rights reserved.
Designed by FTL Wordpress Themes brought to you by Smashing Magazine

Back to Top