Maik Hasler
Maik Hasler

Reputation: 1380

How to use a Java .dll in C# .NET Core 6?

Initial situation

I've made a little test for my project today - The goal: Implement .jar files into a C# project as a .dll. My current .java / .jar file looks like the following.

package ws;

public class Adding
{
    public int Add(int a, int b)
    {
        return a + b;
    }
}

I successfully converted the above into a .dll with IKVM (Version: 7.5.0.2).

I now want to reference this .dll in my C# project and call the Add(int a, int b) method. I already added the reference like so:

enter image description here

Anyways I am not able to call the method, because the compiler can't find the .dll reference..

using Adding; // <= Compiler Error CS0246 (Can't find the reference)

Console.WriteLine(Add(1, 2));

Does anybody know how I could achieve this? I highly appreciate any kind of help, cheers!


Edit 1: Decompiling

I've decompiled the .dll, as demanded in the comments, with ILSpy (Version: ILSpy 7.2), which results into the following output.

// C:\Users\maikh\Desktop\Adding.dll
// Adding, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// Global type: <Module>
// Architecture: AnyCPU (64-bit preferred)
// Runtime: v4.0.30319
// Hash algorithm: SHA1

using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using IKVM.Attributes;

[assembly: Debuggable(true, false)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: SourceFile(null)]
[module: JavaModule(Jars = new string[] { "Adding.jar" })]
[module: PackageList(new string[] { })]

I've also found some references, while decompiling the .dll. I don't know if this is important to mention, but I'll provide it anyways.

// Detected TargetFramework-Id: .NETFramework,Version=v4.0
// Detected RuntimePack: Microsoft.NETCore.App

// Referenced assemblies (in metadata order):
// IKVM.Runtime, Version=7.5.0.2, Culture=neutral, PublicKeyToken=00d957d768bec828
    // Assembly reference loading information:
    // There were some problems during assembly reference load, see below for more information!
    // Error: Could not find reference: IKVM.Runtime, Version=7.5.0.2, Culture=neutral, PublicKeyToken=00d957d768bec828

// mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
    // Assembly reference loading information:
    // Info: Success - Found in Assembly List



// Assembly load log including transitive references:
// IKVM.Runtime, Version=7.5.0.2, Culture=neutral, PublicKeyToken=00d957d768bec828
    // Error: Could not find reference: IKVM.Runtime, Version=7.5.0.2, Culture=neutral, PublicKeyToken=00d957d768bec828

// mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
    // Info: Success - Found in Assembly List

Edit 2: Decompiling V2

I've managed to add the missing reference IKVM.Runtime. Nevertheless I can't find any informations about the namespace, class or method.

enter image description here

Upvotes: 3

Views: 1950

Answers (2)

Olivier
Olivier

Reputation: 18037

Since your Java class is in the ws package, you should be using ws in your C# code:

using ws;

Adding adding = new Adding();
Console.WriteLine(adding.Add(1, 2));

And if you want to call the Add method statically, declare it static in Java:

package ws;

public class Adding
{
    public static int Add(int a, int b)
    {
        return a + b;
    }
}
using ws;

Console.WriteLine(Adding.Add(1, 2));

Upvotes: 2

JonasH
JonasH

Reputation: 36341

First of all, you are using a class as a namespace, and that is probably not correct. Your method call should probably look something like this:

var adder = new Adding();
Console.WriteLine(adder.Add(1, 2));

If that does not work I would inspect the produced dll to verify that it is a conforming .net dll. That should also show the namespaces, class names and other information. A decompiler like dotPeek or ilSpy might show the same information in a format that may be easier to read.

Upvotes: 4

Related Questions