LemonLion
LemonLion

Reputation: 79

Running PlantUml Jar On IKVM fails with System.TypeInitializationException

I am attempting to run PlantUml in .Net rather than relying on a Java Virtual Machine installed locally/in docker/externally for it to run on. IKVM as I understand it creates a JVM that runs in .Net itself, so would be the solution to my problem.

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.

Upvotes: 0

Views: 176

Answers (1)

LemonLion
LemonLion

Reputation: 79

So after discussing it with the IKVM people, turns out that the PlantUml Jar is using AWT which isn't currently supported by IKVM, although they may do in the future. See here: https://github.com/ikvmnet/ikvm/issues/466


Update July 2024: After the latest IKVM Update, solution is now fully working https://github.com/lemonlion/PlantUmlIkvmTest

Upvotes: 0

Related Questions