Search code examples
javac#dlljarikvm

Importing .jar to .dll with IKVM and using it


I'm trying to use a method from external jar in my C# project. So, I have a java project

package externalpackage.srp;

public class stringPair {
    private String a;
    private String b;

    public stringPair(String a, String b) {
        this.a = a;
        this.b = b;
    }
    public String concat() {
        return this.a + this.b;
    }
}

I imported it to dll with IKVM:

ikvmc test.jar

Then I added test.dll to references. Now I'm trying to use it in my C# project.

using System;
using System.Reflection;
using System.Reflection.Emit;

namespace HelloWorld
{
    class Hello
    {

        static void Main()
        {
            string a = "aaa";
            string b = "bbb";
            java.lang.Class clazz = typeof(externalpackage.srp.stringPair);
            java.lang.Thread.currentThread().setContextClassLoader(clazz.getClassLoader());
            object obj = new externalpackage.srp.stringPair(a, b);
            Console.WriteLine(obj.concat());
            Console.ReadKey();
        }
    }
}

And Visual Studio shows error: 'object' does not contain a definition for 'concat' and no extension method 'concat' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

Looks like object created successfully, but contat method somewhy can't be executed. How should I use concat properly?


Solution

  • You simply need to change the type of your variable to stringPair (and not object):

    object obj = new ...

    to

    externalpackage.srp.stringPair obj = new ...