Consulo: Running Java code on a .NET platform using IKVM.NET

    Hi Habr. My previous posts describe only Unity or Mono / Microsoft .NET support . But there are many more things that I don’t talk about. One thing I want to talk about is IKVM.NET.

    For those who missed my posts:
    Consulo is a fork of IntelliJ IDEA Community Edition, which has .NET support (C # at the moment, and there is a lot of emphasis on this sector), Java


    Imagine the situation is that we need to run a Java project under IKVM.NET .

    Let's start with IKVM.NET

    IKVM.NET is an implementation of Java for Mono and the Microsoft .NET Framework. It includes the following components:

    A Java Virtual Machine implemented in .NET
    A .NET implementation of the Java class libraries
    Tools that enable Java and .NET interoperability

    You can find more about IKVM.NET here , read the tutorial but it is boring, and not very usable for developers.

    We put the necessary plugins for Consulo , we need IKVM.NET + Microsoft .NET ( Mono IKVM.NET does not work, the problem is from Xamarin ) and their dependencies. As a result, our list of plug-ins is as follows:

    image.

    Let's get started. Create a Java project, and run Let's see what the println : We switched to PrintStream.class which is in the rt.jar - as usual

    image



    image



    We pass to IKVM.NET


    Now we have a situation - I do not want to lose JVM support , but at the same time I want to be able to run code under IKVM.NET . To copy a project, or lose Java settings, I have no desire. What should we do?

    Under other IDEs - such as Visual Studio or IntelliJ IDEA , you would have to do as described above - but not in Consulo . Consulo has a functionality called Module Layers , it allows you to create several layers of settings for modules, and at the same time quickly switch between them.

    Standard - each module has one layer that has the name Default. .NET projects are created with two layers of the Debug & the Release

    Go to the module configuration and make a copy of the current layer : Call it, for example, IKVM : Now we have two layers : We have a full copy of the Default layer, with support for the Java (in Extensions included the Java ) Turn off Java support, and turn on IKVM.NET : Java SDK must be specified, since IKVM .NET does not have a built-in Java code compiler, and javac is used

    image



    image



    image





    image

    .

    Go higher, customize. NET module extensions: We need to enable Allow Source Roots , and select Main Type - IKVM.NET is not able to select the Entry Point class. As you can see, we can already select the Java class. We also need to add the mscorlib.dll dependency : If we configured everything correctly, try to run the already existing Run Configuration : But this emulates the behavior of java.exe , and during startup it translates the JVM bytecode into the CLI , which guarantees a slow start. IKVM.NET

    image





    image



    image



    It offers compilation of Java code into a * .exe file, which is then launched as a regular .NET application. Therefore, under IKVM.NET, we have another Run Configuration available that launches the .NET App : Run: As you can see, we are already running untitled1.exe and not ikvm.exe , the startup speed is much higher :). Now we try to switch to println : Now we see that we switched to PrintStream.msil , which lies in IKVM.OpenJDK.Core.dll . That is, we are already working with .NET bycode, and for example we can use .NET

    image



    image





    image

    classes (all .NET classes have an initial namespace cli , so as not to conflict with Java classes): And call the methods: Now let's go back to the JVM implementation: As you can see, the code is red, since the JVM and .NET classes are not available.

    image



    image



    image

    image



    PS


    This post - shows how flexible support is, and is not focused only on some kind of technology stack. The IKVM.NET plugin does not know much, for this is not a priority plugin for me.

    In the future I would like to see Debug for Java code on the .NET platform. The challenge there is , and it is global - alas not yet solved for me.

    thanks

    Also popular now: