Category Archives: Uncategorized

Call Java from C#, IKVM

There are not many reasons why someone would want to call Java code from C#. This is like downgrading your code. It would make sense to use a service, but why directly call Java?
I can think of three reasons:
– You have access to the Java code and you do not want to program a service.
– You have access to a Java library. You have no access to its source code.
– You want to directly use the same code from Java and C#, because you are offering your software on several platforms.

There are many ways to get access to Java code. I am concentrating on the easiest one today, which is IKVM.
And just to have it mentioned as well. There is a lot of software in the market that supports the link between Java and C#. Eg. jni4net looks pretty awesome. It is worth checking the links on this page.

Let me give you an easy IKVM example now. This is the Java code:

package MyNameSpace;

public class Friend {
  public int friendSince; // value type
  public String name; // reference type
} // class
// ---------------------------------------------------------------------
package MyNameSpace;

interface IFriends {

  Friend[] getGetFriends();

  String ShowErrorMessage(String xMessage);
} // interface
// ---------------------------------------------------------------------
package MyNameSpace;

import javax.swing.*;

public class Friends implements IFriends {

  @Override
  public final String ShowErrorMessage(String xMessage) {
    System.out.println("Do you see the nice Swing MessgeBox?");
    int lButtons = JOptionPane.showConfirmDialog(null, "Let's use Swing.", xMessage, JOptionPane.YES_NO_CANCEL_OPTION);
    switch (lButtons) {
      case JOptionPane.CANCEL_OPTION:
        return "Cancel? Any other decision would have been better than that one!";
      case JOptionPane.NO_OPTION:
        return "No? What a pessimist you are!";
      case JOptionPane.YES_OPTION:
        return "Yes? PS:I lov ya!";
      default:
        return "impossible";
    }
  } //

  @Override
  public final Friend[] getGetFriends() {
    Friend[] lFriends = new Friend[6];
    for (int i = 0; i < 6; i++) lFriends[i] = new Friend(); 
    
    lFriends[0].name = "Michael Sylvester Gardenzio Stallone";
    lFriends[0].friendSince = 1946;

    lFriends[1].name = "Rocky Balboa";
    lFriends[1].friendSince = 1978;

    lFriends[2].name = "John Rambo";
    lFriends[2].friendSince = 1982;

    lFriends[3].name = "Ray Breslin";
    lFriends[3].friendSince = 2013;

    lFriends[4].name = "Gabe Walker";
    lFriends[4].friendSince = 1993;

    lFriends[5].name = "Ray Tango";
    lFriends[5].friendSince = 1989;

    return lFriends;
  } //
  
} // class

Compile above code into a jar file.
To keep things simple, copy the jar file (here: JavaLib.jar) to your IKVM folder, which is F:\Programming\ikvm-7.2.4630.5\bin in our example. Now convert the file into C# by entering the following command:

IKVM

It is important to use the right Java Version. I had Java 8 installed, but had to use JDK1.7 to avoid incompatibility. The 32/64 bit issue is also important. Use the right version in the -plattform: option. Simply type in IKVMC to see the usage of the IKVMC command.

Now add the reference IKVM.OpenJDK.Core to your C# project. You also need to reference your library, which is JavaLib.dll in our example.

References

It could not be easier. This was it!
Let’s run a test program:

using System;

namespace CallJava {
   class Program {
      
      static void Main(string[] args) {         
         MyNameSpace.Friends lFriendsObject = new MyNameSpace.Friends();
         string lFeedback = lFriendsObject.ShowErrorMessage("Hello Java from C#");
         Console.WriteLine("C# says that Java replied: " + lFeedback);
         MyNameSpace.Friend[] lFriends = lFriendsObject.getGetFriends();
         foreach (MyNameSpace.Friend lFriend in lFriends) {
            Console.WriteLine("since " + lFriend.friendSince + " " + lFriend.name + " is one of my best buddies");
         }
         Console.ReadKey();
      } //

   } // class
} // namespace

<a href="https://csharphardcoreprogramming.files.wordpress.com/2014/04/messagebox.jpg"><img src="https://csharphardcoreprogramming.files.wordpress.com/2014/04/messagebox.jpg" alt="messagebox" width="268" height="112" class="alignnone size-full wp-image-1499" /></a>

example output:
Do you see the nice Swing MessgeBox?
C# says that Java replied: Yes? PS:I lov ya!
since 1946 Michael Sylvester Gardenzio Stallone is one of my best buddies
since 1978 Rocky Balboa is one of my best buddies
since 1982 John Rambo is one of my best buddies
since 2013 Ray Breslin is one of my best buddies
since 1993 Gabe Walker is one of my best buddies
since 1989 Ray Tango is one of my best buddies

Advertisements