📜 ⬆️ ⬇️

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

Hi Habr. My previous posts describe only support for Unity or Mono / Microsoft .NET . But when there is still a lot of things about which I do not tell. I want to tell you about one thing, this is IKVM.NET

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


Imagine a situation that we need to run a Java project under IKVM.NET .
')
Let's start first with IKVM.NET

IKVM.NET is a 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’s boring and not very usable for development.

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 plugins looks like this:

image .

Let's get started Create a Java project, and run

image

Let's see what println is :

image

We switched to PrintStream.class which is in rt.jar - as usual

Go to IKVM.NET


Now we have a situation - I don’t want to lose JVM support, but at the same time I want to be able to run the code under IKVM.NET . Copy the 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 it as described above - but not in Consulo . Consulo has a functionality called Module Layers , it allows you to create several layers of settings for the modules, and at the same time quickly switch between them.

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

Go to the settings of the module, and make a copy of the current layer :

image

Call it, for example, IKVM :

image

Now we have two layers :

image

We have a complete copy of the Default layer, with Java support ( Java is included in Extensions )

Turn off Java support, and enable IKVM.NET :

image

The Java SDK must be specified, since IKVM .NET does not have a built-in Java code compiler, and javac is used.

Go higher, customizable. NET module extensions:

image

We need to enable Allow Source Roots , and select Main Type - IKVM.NET cannot select the Entry Point class. As you can see, we can already choose a Java class.

We also need to add the mscorlib.dll dependencies:

image

If we configured everything correctly, we try to run an already existing Run Configuration :

image

But it emulates the behavior of java.exe , and during startup translates the JVM bytecode to the CLI , which guarantees a slow start.

IKVM.NET offers compilation of Java code into a * .exe file, which is then launched as a normal .NET application. Therefore, under IKVM.NET, we have another Run Configuration available that launches the .NET App :

image

Run:

image

As we see, we are already launching untitled1.exe and not ikvm.exe , the launch speed is much higher :).

Now we are trying to switch to println :

image

Now we see that we have moved to PrintStream.msil , which lies in IKVM.OpenJDK.Core.dll . That is, we already work with the .NET buycode, and for example we can use the .NET classes (all .NET classes have an initial namespace cli , so as not to conflict with the Java classes):

image

And call methods:

image

Now let's go back to the JVM implementation:

image

image

As you can see - the code is red, since the JVM , .NET classes are not available.

PS


This post shows how flexible the support is and does not focus only on any technology stack. IKVM.NET plugin does not know a lot of things, because it is not a priority plugin for me.

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

thank

Source: https://habr.com/ru/post/262169/


All Articles