Calling Java “Hello World” from .NET in simple C# Console Application
With Javonet, .NET developers can easily invoke Java methods from .NET applications by referencing Java JARs, setting and getting fields, passing arguments, and retrieving results. Here’s a quick guide with code to help you get started.
You can also check this sample in our GitHub repository:
https://github.com/Javonet/tutorials/tree/master/DotNet/HelloWorldFromJavaConsoleApp
Folders structure
We will take care of the folder structure first to keep everything organised. In our main folder (we’ve named it HelloWorldFromJavaConsoleApp) we should create two folders:
- JavaHelloWorld – this will containt Java files
- NetCoreConsoleApp – this will be .NET Core main folder
HelloWorldFromJavaConsoleApp ├── JavaHelloWorld ├── NetCoreConsoleApp └── .gitignore
Java Code (SampleJavaClass.java)
We should now move to JavaHelloWorld folder and create SampleJavaClass.java file. Inside that file, we create a simple static method called “HelloWorld”, that returns “Hello World from Java!” greeting
public class SampleJavaClass { public static String HelloWorld() { return "Hello World from Java!"; } }
This Java code has to be compiled. In order to do this, you have to run this command from terminal from JavaHelloWorld folder:
javac *.java
Now, package the .class file into a JAR. Run the following command:
jar cvf JavaHelloWorld.jar *.class
After these steps, you should see JavaHelloWorld.jar file in your folder.
C# Code (Program.cs)
Now let’s move to our .NET folder (NetCoreConsoleApp) and run the command from terminal:
dotnet new console
This should create Program.cs file, which will be our entry point for the application. We now need to add Javonet SDK to the project. In the same terminal, run this command:
dotnet add package Javonet.Netcore.Sdk
Now we can edit Program.cs file. The C# example demonstrates using Javonet to call Java methods within a .NET console application. Each line has comments, explaining what is happening and Key Points, which can be found below the code.
// Activate Javonet using your license key Javonet.Netcore.Sdk.Javonet.Activate("n9B5-Km7g-Pp69-j9FE-e9A5"); // Initialize Java runtime environment var rtmCtx = Javonet.Netcore.Sdk.Javonet.InMemory().Jvm(); // Load Java JAR file rtmCtx.LoadLibrary("../JavaHelloWorld/JavaHelloWorld.jar"); // Access Java class var javaType = rtmCtx.GetType("SampleJavaClass").Execute(); // Call HelloWorld method var result = javaType.InvokeStaticMethod("HelloWorld").Execute(); // Show the result (Hello World) Console.WriteLine(result.GetValue());
Key Points
- Javonet Activation: Call Javonet.Activate with your license key. This only needs to be done once in the application.
- Java Runtime Initialization: Use Javonet.InMemory().Jvm() to initialize the Java runtime.
- Library Loading: LoadLibrary method points to the JAR file containing your Java code.
- Method Invocation: Use InvokeStaticMethod to call static methods on the Java class, passing arguments as needed.
The final structure of the project should look like this:
HelloWorldFromJavaConsoleApp ├── JavaHelloWorld │ ├── JavaHelloWorld.jar │ ├── SampleJavaClass.class │ └── SampleJavaClass.java ├── NetCoreConsoleApp │ ├── NetCoreConsoleApp.csproj │ └── Program.cs └── .gitignore
Sample Source Code
This simple example demonstrates how easy it is to call Java methods from .NET and achieve cross-platform integration with just a few lines of code and minimal setup.
You can also checkout or fork this sample from our GitHub repository:
https://github.com/Javonet/tutorials/tree/master/DotNet/HelloWorldFromJavaConsoleApp