ikvmnet / ikvm

A Java Virtual Machine and Bytecode-to-IL Converter for .NET
Other
1.15k stars 110 forks source link

java.lang.NoSuchMethodError thrown while calling a method #431

Closed ubythulla closed 8 months ago

ubythulla commented 8 months ago

Below are the sample java code

   package Main;

   import Calculate;

   public class calculator
   {      
       public int Calculate(int a, int b)
       {
          return Addition.Sum(a,b);
       }
   }

another class from another package

   package Calculate;
   public class Addition
   {      
       public static int Sum(int a, int b)
       {
           return a+b;
       }
   }

And, I reference this from a C# console application:

public static void main()
    {      
     Main.calculator calc=  new Main.calculator();
     int sumVal=calc.Calculate(1,2);
    }

throws error as

Exception thrown: 'java.lang.NoSuchMethodError' in Main.dll

IKVM tried to find the static Sum method from Main dll instead of Calculate dll.

In the console app, I inserted a classloader as seen below, but that did not fix the problem. Same error only thrown

    public static void main()
    {      
     string CalculatePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Calculate.dll");
     Assembly CalculateAssembly = Assembly.Load(AssemblyName.GetAssemblyName(CalculatePath ));
     ikvm.runtime.Startup.addBootClassPathAssembly(CalculateAssembly );

     Main.calculator calc=  new Main.calculator();
     int sumVal=calc.Calculate(1,2);
    }

I added the jars directly to the dot net project and added ikvm reference in the csproj file as like below

<ItemGroup>
    <IkvmReference Include="Main.jar">
      <AssemblyName>Main</AssemblyName>
      <AssemblyVersion>2.5</AssemblyVersion>
      <AssemblyFileVersion>2.5.0.0</AssemblyFileVersion>
     <References>Calculate.jar</References>
    </IkvmReference>
  </ItemGroup>
  <ItemGroup>
    <IkvmReference Include="Calculate.jar">
      <AssemblyName>Calculate</AssemblyName>
      <AssemblyVersion>2.0.7</AssemblyVersion>
      <AssemblyFileVersion>2.0.7.0</AssemblyFileVersion>
    </IkvmReference>
  </ItemGroup>

Your help will be much appreciated, we are stuck here and not able to proceed further. Thanks in advance.

wasabii commented 8 months ago

Stack trace please.

ubythulla commented 8 months ago

Stack trace

 at Main.Calculate.calculator(int a, int b) in C:\Users\Documents\Visual Studio 2019\Projects\Test\Program.cs:line 5

Exception messgae

Calculate.Addition.Sum(II)LCalculate.Addition;

Output window error message

Exception thrown: 'java.lang.NoSuchMethodError' in Main.dll
wasabii commented 8 months ago

Ok. Chances are the body of the calculator method was replaced with NoSuchMethodException at compile time. The first time building you probably had warning messages about missing classes or missing methods.

You can find those previous log files along side the assemblies which were generated in the IKVM cache directory. Which will be in your temp directory/ikvm

ubythulla commented 8 months ago

This is sample code only, but I can see that several classes are missing in the original code. Would the NoSuchMethodException be resolved if I fixed the class missing errors?

wasabii commented 8 months ago

Likely, yes.

When using static compilation (which is what you're using, converting a JAR to a DLL), IKVM has to be able to resolve all the types that would otherwise be needed at runtime in Java, since those types have to be baked into assemblies at that point in time. If it can't resolve one, it tries to gracefully fall back, generating what it can, and generating code that throws NoSuchMethod just like Java would do at runtime.

ubythulla commented 8 months ago

Ok. I appreciate you very much helping me understand this. I will try to fix the missing class errors.

wasabii commented 8 months ago

Closing. User can reopen if a bug still exists. It sounds like he was just building things in the wrong order.

ubythulla commented 3 months ago

I am still having this issue

I added the jars directly to the dot net project and added ikvm reference in the csproj file as like below

<ItemGroup>
    <IkvmReference Include="Main.jar">
      <AssemblyName>Main</AssemblyName>
      <AssemblyVersion>2.5</AssemblyVersion>
      <AssemblyFileVersion>2.5.0.0</AssemblyFileVersion>
    </IkvmReference>
  </ItemGroup>

I can able to access the java method which comes from the Main.jar in the C# without any error as like below

common.net.util.ConvertUtils.convertFromEntityList(rules,  seqState);

During the run time of application I am getting the error and the cache file also contains the same error.

ERROR: warning IKVMC0117: Emitted java.lang.NoSuchMethodError in "common.net.util.ConvertUtils.convertFromEntityList(Ljava.util.List;Lcommon.net.util.ConvertUtils$SequenceState;)Ljava.util.List;"