3

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

Olivier
  • 13,283
  • 1
  • 8
  • 24
Maik Hasler
  • 1,064
  • 6
  • 36
  • Try decompiling the DLL to C# with a tool like ILSpy. Seeing the C# code will help people diagnose your issue. – Reilly Wood Mar 03 '22 at 16:59
  • 1
    You seem to have a missing reference to "IKVM.Runtime", that will need to be fixed, but should only cause runtime errors. You should have your Adding class in the ILSpy menu tree for the assembly, that would be relevant to include if you still get compiler errors. – JonasH Mar 04 '22 at 07:46
  • @JonasH I've no experience with ILSpy or decompiling in general - How do I add the Adding class in the menu tree for the assembly? Thanks already – Maik Hasler Mar 04 '22 at 08:56
  • 1
    You should not have to add anything. The tree for the assembly should expand into namespaces, classes, methods etc... If you cannot find your class in the tree then something is wrong with the conversion. Or at least, the produced assembly cannot be used as a regular .net assembly. See [ILSpy Screenshots from store page](https://www.microsoft.com/en-us/p/ilspy/9mxfbkfvsq13?activetab=pivot:overviewtab#) – JonasH Mar 04 '22 at 09:11
  • @JonasH I've managed to add the missing reference "IKVM.Runtime". Nevertheless I can't find the namespace, class, method etc... (see the edit #2) – Maik Hasler Mar 04 '22 at 13:10
  • I can't help you with that, as I'm not familiar with IKVM. – JonasH Mar 04 '22 at 13:23
  • Did you try with IKVM 8.5? And what version of Java did you use to compile the jar? – Olivier Mar 06 '22 at 13:28
  • I didn't managed to convert my `.jar` with the 8.5.0.2 Version, but I've tried it with the [newest version of IKVM](https://github.com/jessielesbian/ikvm/releases/tag/v8.6.9.0_RC6) I could find. Nevertheless same problem. I am using Java 8 – Maik Hasler Mar 06 '22 at 13:43

2 Answers2

4

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.

JonasH
  • 28,608
  • 2
  • 10
  • 23
2

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));
Olivier
  • 13,283
  • 1
  • 8
  • 24
  • Thank for asking about my Java verison - Just found out that my private computer hasn't installed Java... No wonder why it is not working. It's strange that IKVM doesn't throw any error. Long story short it is working now. Anyways I do have new problems to solve now^^ I don't know if you want to make a suitable answer for the Java problem? - So I can mark it as helpful. Small reminder never switch between working computer and private computer^^ – Maik Hasler Mar 06 '22 at 14:19