ikvmnet / ikvm

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

Could not load type 'System.Reflection.Emit.MethodToken' from assembly mscorlib #518

Closed sanjeev-saxena-us closed 1 month ago

sanjeev-saxena-us commented 2 months ago

Hi

I am working on a .net 8 Kubernetes service that needs to use a collection of jar files. Most of the Java classes get converted but when I try to new up a specific class in the jar file, I get the "Could not load type 'System.Reflection.Emit.MethodToken' from assembly mscorlib" exception. I realize that this class is in the .net framework which is blocking us from moving forward since the service will be hosted in a linux container in Azure Kubernetes. Is there a fix for this or a workaround?

Thanks

wasabii commented 2 months ago

This sounds like a Framework assembly trying to be used on Core.

sanjeev-saxena-us commented 2 months ago

The jar files I mentioned are an outside vendor's which is being used in one of our legacy Java apps. We need to use external vendor's jar files in our new .net8/C# app.

We did two options:

  1. We ran the IKVMC utility to create a single .net dll from these jar file which worked. We referenced the created dll in the .net8 project and compiled which also worked. Then we new upped a few classes in the dll which also worked. However when we tried to new up a different class, we got the exception
  2. When we downloaded the IKVM nuget package, the class which resulted in the exception was not available.

Here is a link to someone else's experience on this from last year: https://github.com/KevM/tikaondotnet/issues/156 Are you saying that IKVM is using .net framework?

Thoughts?

wasabii commented 2 months ago

We ran the IKVMC utility to create a single .net dll from these jar file which worked. We referenced the created dll in the .net8 project and compiled which also worked. Then we new upped a few classes in the dll which also worked. However when we tried to new up a different class, we got the exception

You ran the wrong version of the tool, likely. You ran the Framework version, against the Framework libraries, and created a Framework assembly.

Please use IkvmReference.

sanjeev-saxena-us commented 2 months ago

Yes, that was the option 2 in my comment earlier.

<ItemGroup>
    <PackageReference Include="IKVM" Version="8.8.0" />
</ItemGroup>
<ItemGroup>
    <IkvmReference Include="some1.jar">
      <AssemblyName>some1</AssemblyName>
      <AssemblyVersion>1.0.0.1</AssemblyVersion>
          <AssemblyFileVersion>1.0.0.1</AssemblyFileVersion>
    </IkvmReference>
    <IkvmReference Include="some2.jar">
      <AssemblyName>some2</AssemblyName>
      <AssemblyVersion>1.0.0.1</AssemblyVersion>
          <AssemblyFileVersion>1.0.0.1</AssemblyFileVersion>
    </IkvmReference>
    <IkvmReference Include="some3.jar">
      <AssemblyName>some3</AssemblyName>
      <AssemblyVersion>1.0.0.1</AssemblyVersion>
          <AssemblyFileVersion>1.0.0.1</AssemblyFileVersion>
    </IkvmReference>
    <IkvmReference Include="some4.jar">
      <AssemblyName>some4</AssemblyName>
      <AssemblyVersion>1.0.0.1</AssemblyVersion>
          <AssemblyFileVersion>1.0.0.1</AssemblyFileVersion>
    </IkvmReference>
</ItemGroup>

The class in the some2.jar that threw the exception was missing

wasabii commented 2 months ago

During build, what errors or warning were output?

If you cannot reproduce it, you can check the .log file left behind in %TEMP%\ikvm\cache\1

sanjeev-saxena-us commented 2 months ago

There are no build errors. Also I do not see a ikvm folder in the %TEMP% folder

wasabii commented 2 months ago

I'm going to bet there is and there were.

sanjeev-saxena-us commented 2 months ago

Likely, yes; its just that the Visual Studio 2022 build completes successfully (the output window reflects no errors). Where else can the ikvm folder be created?

wasabii commented 2 months ago

%TEMP% is pretty much the only place it should be unless you managed to figure out how to override it with IkvmCacheDir in your project file.

It's where the built DLL files go.

sanjeev-saxena-us commented 2 months ago

Found the cache folder (for some reason it was hidden) - thanks for pointing me in this direction! You were right in that the error were in the log file (VS build actually succeeded without errors). I'll look at the IKVM generated log files to determine where the issue(s) are and report back.

wasabii commented 2 months ago

The errors/warnings are emitted on the first build. But since IkvmReference caches the result, you only see them the first time.

sanjeev-saxena-us commented 2 months ago

To delete the cache and generate from the latest, do I simply delete the "ikvm" folder or the "cache" folder?

wasabii commented 2 months ago

It always generates a new assembly if material changes were made. But, you can delete the folder to force it.

sanjeev-saxena-us commented 1 month ago

Sorry for the delay in responding; its working just fine.