ikvmnet / ikvm

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

Running Jar On IKVM fails with System.TypeInitializationException #466

Closed lemonlion closed 5 months ago

lemonlion commented 6 months ago

I am attempting to run the PlantUml Jar on IKVM, but it seems to just fail with a System.TypeInitializationException

To aid anyone who want to try help fixing the issue I have created a public repository of the code https://github.com/lemonlion/PlantUmlIkvmTest

First I went to https://plantuml.com/download and downloaded the latest MIT licensed version (plantuml-mit-1.2023.13.jar). I then created a new C# .Net 6 console project named PlantUmlIkvm and added a new folder called PlantUml and put the jar in there.

Next I installed all the packages mentioned in the IKVM Readme and I also installed IKVM.Maven.Sdk

    <ItemGroup>
        <PackageReference Include="IKVM" Version="8.7.3" />
        <PackageReference Include="IKVM.Image" Version="8.7.3" />
        <PackageReference Include="IKVM.Image.JDK" Version="8.7.3" />
        <PackageReference Include="IKVM.Image.JRE" Version="8.7.3" />
        <PackageReference Include="IKVM.Maven.Sdk" Version="1.6.5" />
        <PackageReference Include="IKVM.MSBuild" Version="8.7.3" />
    </ItemGroup>

Next I added an IkvmReference tag in my project file as per the IKVM readme. I also put Debug true to allow for any debugging functionality.

    <ItemGroup>
        <IkvmReference Include="PlantUml\plantuml-mit-1.2023.13.jar" >
            <Debug>true</Debug>
        </IkvmReference>
    </ItemGroup>

At this point the entire solution looks like this:

enter image description here

enter image description here

I then rebuilt the entire solution. I look in C:/Code/PlantUmlIkvm/bin/Debug/net6.0 and am pleased to see that a dll has been created called net.sourceforge.plantuml.dll indicating that IKVM has successfully translated the jar into something .net compatible.

enter image description here

I then attempt to use PlantUml from my solution. Besides some intellisense weirdness (the IDE isn't always recognising the classes as classes), the new classes appear to be picked up by visual studio and I can decompile into them using Visual Studio/Resharper. I follow the PlantUml tutorial from their main page for creating a PNG image from some basic plantUml and so I enter the following code into the main Program.cs:

    using System.Reflection;
    using com.plantuml.api.cheerpj;
    using com.plantuml.api.cheerpj.v1;
    using java.io;
    using net.sourceforge.plantuml;
    using net.sourceforge.plantuml.api;
    using net.sourceforge.plantuml.security;
    using net.sourceforge.plantuml.sequencediagram;
    using sun.misc;
    using sun.net.www.content.image;

    namespace PlantUmlIkvm;

    public class Program
    {
        public static void Main(string[] args)
        {
            var uml = "@startuml\n";
            uml += "Bob -> Alice : hello\n";
            uml += "@enduml\n";
            var reader = new net.sourceforge.plantuml.SourceStringReader(uml);
            OutputStream png = new Base64OutputStream();
            var desc = reader.outputImage(png).getDescription();
        }
    }

The program builds successfully. It runs successfully up to a point, but fails on this line:

var desc = reader.outputImage(png).getDescription();

The line fails with message

System.TypeInitializationException: 'The type initializer for 'net.sourceforge.plantuml.FileFormat' threw an exception.'

enter image description here

enter image description here

Outer Exception TargetSite:

enter image description here

Inner Exception TargetSite:

enter image description here The main Stack Trace:

enter image description here

The inner exception Stack Trace:

enter image description here

I believe the error is happening when it tries to create a FileFormatOption, hence when I insert this additional line it fails there first:

var fileFormat = new FileFormatOption(FileFormat.PNG);

enter image description here

I am running all this on Windows 10.

I imagine this is more of an IKVM issue than a PlantUml issue, but open to any ideas. Although the IKVM Readme is a good baseline, I can't see any real world examples there so difficult to know if I'm using IKVM correctly or not.

wasabii commented 6 months ago

Looks like this goes into AWT, which we don't currently support.

wasabii commented 6 months ago

144

lemonlion commented 6 months ago

@wasabii Ah ok - thanks for your reply. Is there any workaround at all that you could suggest?

wasabii commented 6 months ago

Nope. Not as it stands currently. There's probably a possibility of getting headless mode working, but I'm just not sure what that requires right now.

wasabii commented 5 months ago

Closing since duplicate.