Photo by Perry Kibler on Unsplash

In this article I present another way to use any JVM enabled language from .NET: the project JNet, hosted on GitHub. The project exposes almost the full API surface of Java 8 to any .NET user, but not only this.

Why a project like this?

The answer to the question is in the history of another project: KNet. KNet is the porting of the Apache Kafka Java APIs to .NET and I will write deeply on it in another article. Well, originally the first project done was KNet and Apache Kafka Java APIs were written in C# with a big developing effort. Looking in the code many classes used from Apache Kafka are the one of the JVM used to run it. So the idea was to move the Java classes to a new project dedicated to that scope and use it as dependency of KNet. The first incubation of JNet was done using the classes used from KNet.

Evolution

The first published package was very poor, however many needs were discovered and the roadmap was updated with other features:

  • A command line interface (JNetCLI) was added to interact with the underlying library in many ways: REPL and scripts written in C# that interact with the JVM.
  • An extension to PowerShell (JNetPS) to interact with the JVM using PowerShell scripting language.

Anyway, the core library (JNet) was always manually made with a big development effort. So the last, but not least, project added is JNetReflector: a tool able to use the reflection to write the C# classes corresponding to the one in JVM.

With JNetReflector the effort to build the C# classes was reduced and JNet now contains almost the full API surface of Java 11. Since JNetReflector use the reflection it is able to reflect any language based on the JVM like Scala, Kotlin and so on.

Now the project motto is:

.NET suite for Java™/JVM™

What can be done with JNet?

There are many ways to use JNet, here a non exhaustive list from the current usages. I try to divide it based on the specific project:

  • JNet: this is the core library and it can be integrated within other .NET project to use directly the JVM APIs. The core library itself can be used to interact with any class in JVM using the features available and described in API extensibility article. Currently, KNet project use the library to avoid generation of Java APIs. Other features available are related to the graphical components of Java (AWT/Swing): they can be allocated standalone from .NET or integrated within .NET based User Interfaces: for anyone curious the templates contains usage examples about this features.
  • JNetPS: the project contains some basic cmdlet to instantiate JVM classes and use them, plus many specific cmdlet targeting JMX. This can help any user of PowerShell to interact with the JMX APIs to retrieve information and use it within PowerShell scripts.
  • JNetCLI: could be used to interact with the JVM from any command prompt with REPL, C# scripts or can load code files to be executed mixing C# and requests to JVM.
  • JNetReflector: currently it is used to generate the reflected classes of the JNet core library, and there is a specific task to use it to fully regenerate the classes manually made in KNet project. Anyway, any user can use it to reflect the classes of any JAR he wants to use from .NET. Think about a JAR (maybe some package from any Maven repository, e.g. Maven Central) or a project made in Java we want to use from .NET. It can be possible to use directly JNet and its API extensibility, anyway JNetReflector comes to help us: just launch the tool targeting the JAR and the .NET code is ready for you. The last is the option will be used from KNet to reflect the Java APIs of Apache Kafka: this approach guarantee that any update on the JAR can be immediately reported to .NET; with an automatic pipeline any upgrade of the JAR (e.g. someone updates or improves the Maven package) can be converted into .NET code with a limited effort.

The way JNet works

JNet interacts with the JVM and does not rewrite it. What does it means? When a JVM API is invoked from C#, the code is executed in the real JVM running within the .NET process. Behind the scene: during .NET process initialization a JVM is started within the .NET process. Both .NET and JVM lives alone with their own threads, Garbage Collector and so on, but can interact together: JNet invokes the JVM methods, while JVM can send back events to .NET.

Benefits and drawbacks

The approach followed from JNet guarantee multiple benefits than other solutions available with some possible drawbacks. I try to list some of them:

  • Runtime identification: methods and fields in JVM classes are identified at runtime and not linked at compile-time; when a method is invoked the first time, JNet needs a longer time-slot to identify the method, then it is cached for subsequent invocation; this behavior allows to use JVM classes even if they are unknown till their invocation and it is the principle behind API extensibility.
  • In-process communication: JVM and .NET communicate within the same process and this guarantee that the communication does not need external channels (marshalling is avoided) and the attack surface is reduced to the minimum; an approach made using other kind of communication channels (like TCP in some solutions) needs marshalling and can expose the final product to cyber-attacks.
  • JNet needs an external JDK/JRE to work: this can be a limitation that other solutions solves with a JVM rewritten for .NET, however with this approach the user of JNet has the freedom to choose the best JVM for his needs: version, supplier and so on; furthermore JNet inherits the cybersecurity characteristics of the chosen JVM.
  • Direct usage: continuing from the above point there is no need of byte-code to IL conversion to use the classes of JVM; this implies that JNet does not need any other intermediate layer which can be source of errors and wrong behaviors.

Just a usage example

The repository contains some example codes. Here I put a code, taken from the available templates, which shows an AWT application that interacts with C#:

namespace MASES.JNetTemplate.JNetAWTApp { class MyJNetCore : JNetCore<MyJNetCore> { } class Program { static void Main(string[] args) { MyJNetCore.CreateGlobalInstance(); // this call prepares the environment: it is mandatory to initialize the JVM var appArgs = MyJNetCore.FilteredArgs; // contains the remaining arguments, the JNet and JCOBridge arguments are discarded try { var vApp = new JVMWrapperApp(); vApp.Execute(); } catch (Exception e) { Console.WriteLine(e.Message); } } class JVMWrapperApp { bool execute = true; void ActionDone(ActionEvent args) { if (args.ActionCommand == "writeToConsole") { Console.WriteLine("Action from JVM"); } else if (args.ActionCommand == "closeApplication") { Console.WriteLine("Closing..."); execute = false; } } public void Execute() { using (var listener = new ActionListener(ActionDone)) { var frame = new Frame("My First GUI"); frame.SetSize(300, 300); var buttonWrite = new Button("Write to console") { ActionCommand = "writeToConsole" }; buttonWrite.AddActionListener(listener); var buttonClose = new Button("Close application") { ActionCommand = "closeApplication" }; buttonClose.AddActionListener(listener); var panel = new Panel(); var layout = new GridLayout(1, 2); panel.Layout = layout; panel.Add(buttonWrite); // Adds Button to content pane of frame panel.Add(buttonClose); // Adds Button to content pane of frame frame.Add(panel); frame.SetVisible(true); Console.WriteLine("Waiting for close..."); while (execute) { System.Threading.Thread.Sleep(10); } } } } } } 

Now I try to explain the above code because in my opinion it demonstrates many capabilities:

  • The code creates a simple user interface, based on AWT, which interacts with the .NET process started it; the ability to create an User Interface is one of the features that JNet gives to its users.
  • After initialization, the first step is to create a listener for events from the JVM; this is another feature available in JNet: a JVM can reacts and stimulates the .NET side.
  • All elements of the user interface are built like it is done in any Java code, then are joined together; JNet creates JVM objects like it is done for any other .NET object, the only difference is where the instance lives.
  • A single listener is attached to both Button objects involved in the graphic layout built; the listener lives both in the JVM and in .NET, and it is possible to use a single instance like a Java code can do.
  • The JVM objects are created like it is done in C# for .NET objects: just use a simple new call.
  • The objects in JVM can interact with the .NET code: the listener used to listen for events from JVM use a .NET callback to execute the actions in the .NET program.
  • The C# code is very similar to an equivalent Java code.

The image shows the code running

Above an image showing the code running:

  • The console shows the output of the action stimulated from “Write to console” button; the trigger is in JVM (Button), the final action is in the .NET code (ActionDone callback).
  • The Java AWT User Interface runs alone than the hosting application made in .NET: making the example more reach, instead to loop waiting for close, the .NET side can do anything and receives events from JVM or invokes the JVM for other actions to be done.

JVM or Java: a bit of clarification

In the article I’m using sometime the term JVM and sometime the term Java. Most of the user knows the difference between them, but I want to explain why I used both. JNet and its tools interacts with the JVM hosted in the .NET process, so generally speaking the best term to be used is JVM. This means that JNet is able to manages every programming language supported from the JVM and Java is the first of all. Specifically, I used Java when the counterpart is a class generated from Java or available in the Java framework, like the Button class of AWT framework.

JNetReflector in action

The code reported in the article uses some classes made in JNet using JNetReflector. An example is Button, which is part of the AWT framework, and its source code is available at https://github.com/masesgroup/JNet/blob/master/src/net/JNet/Generated/Java/Awt/Button.cs.

Starting from the meaning of what is the JVM, with JNetReflector it is possible to convert JVM classes (compiled from Java, Scala, Kotlin, etc) into .NET classes which reproduce them in this way:

  • Fields are converted into C# properties: in all cases they are readable and becomes writable if the JVM counterpart meets the right conditions;
  • Methods are converted into static, or instance, properties if they follow the getter/setter pattern in the JVM;
  • Otherwise methods becomes both static, or instance, methods of the C# class;
  • Generics are managed following the pattern found in Java;
  • Nested classes are managed as nested classes in C#.

JNet itself represents JNetReflector in action: most of the classes available in JNet are reflected using JNetReflector, few of them (mainly classes in java.lang.* package) are manually managed because they are shared between JNet and JNetReflector.

With JNetReflector every JAR, or module, can be converted into C# classes; this approach permits to:

  • converts the JAR into an assembly;
  • the original JAR, or an updated version of it, can be used without the need to regenerate the assembly each time;
  • there is no need to make a byte-code to IL conversion each time a new JAR is available.

Other features

There are other features available, however I’ll simply list them, an explanation will be done in other articles dedicated to each specific topic:

  • JNet supports many options to define how the JVM shall be loaded and started, from memory, to debug, to JNI, etc
  • The example in the article shows an AWT application built in code and running alone, however:
    • the same application can be integrated within a WinForms, or WPF, application as an embedded object of the hosting application: so .NET and AWT/Swing can interact both in graphic and in console applications;
    • an existent Java application made in AWT/Swing can be started and integrated from .NET;
    • an existent Java application made in AWT/Swing can be integrated from a WinForms, or WPF, application.

Conclusion

Update — 2026: Three years have passed since this article was published. JNet has evolved substantially — new performance numbers, a two-gate event filter, bulk data transfer APIs, and a much more mature JNetReflector. If you want to see where the project stands today, read the follow-up: JNet in 2026: the JVM and .NET, side by side — three years later.

In the article I wrote an overview of JNet touching many points. Other article will analyze deeply some features or the usage of JNet suite from projects like KNet.