The following steps are an initial guide to use JCOBridge package available as NuGet package. The same package can be used both from .NET and Java/JVM, however the setup shall start from .NET.
Mandatory Tools
.NET is a mandatory tool shall be available in the development machine; before start any of the following operations install, on the development machine, any version of .NET, preferably the current LTS which is .NET 8: follow the Microsoft instructions in case.
Another mandatory tool is a JDK: choose the JDK of your choice and follow the instructions from the vendor. To simplify the usage of this guide, be aware to set the JAVA_HOME environment variable both following the guide of the vendor or manually.
Step to create your first JCOBridge project
Let’s go starting coding!
The first step needs a project to be available: .NET helps us with a standard way to create a new project. Open a shell and execute the following commands in a location of your choice:
mkdir MyFirstProject
cd MyFirstProject
dotnet new console
The previous steps creates a simple Hello World! project, executing the following command:
dotnet run
you will see the expected output:
Hello, World!
Now it is the moment to start with JCOBridge. Add the JCOBridge package with the following command:
dotnet add package MASES.JCOBridge
The previous command adds the latest version of JCOBridge, to add a specific version use the following:
dotnet add package MASES.JCOBridge --version 2.6.3
Now that the project knows JCOBridge, it’s time to move on to code.
Open the file named Program.cs and replace the content with the following, which is a reviewed version of the code available in simple dotnet example:
using MASES.JCOBridge.C2JBridge;
using MASES.JCOBridge.C2JBridge.JVMInterop;
using System;
namespace JavaClassUseExample
{
class TestClass : SetupJVMWrapper<TestClass>
{
public void Execute()
{
var cls = JVM.New("java.lang.String", "Hello, ") as IJavaObject;
var retString = cls.Invoke("concat", "World!");
Console.WriteLine(retString);
}
}
class Program
{
static void Main(string[] args)
{
try
{
new TestClass().Execute();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine("Press any key.");
Console.ReadKey();
}
}
}
}
Execute again the following command:
dotnet run
and you will get the same output fully generated from the JVM:
Hello, World!
Just a tip: the previous step needs an environment variable named JAVA_HOME as stated before.
Start using the JVM
To use JCOBridge from the JVM follow the following steps.
First of all, a piece of Java code is needed. Create a file named JavaUseExample.java:
import java.io.IOException;
import org.mases.jcobridge.*;
public class JavaUseExample {
public static void main(String[] args) {
try {
try {
try {
JCOBridge.Initialize(args);
} catch (JCException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
//declare and create JCOBridge instance
JCOBridge bridge;
bridge = JCOBridge.CreateNew();
// GENERATE Object
JCType type = (JCType) bridge.GetType("System.String");
//Invoke the C# class methods
java.lang.String helloWorld = (java.lang.String) type.Invoke("Join", ",", "Hello", " World!", " From .NET");
System.out.println(helloWorld);
} catch (JCException jce) {
jce.printStackTrace();
System.out.println("Exiting");
return;
}
}
}
To simplify the next commands, execute the following to create a subfolder with the needed libraries:
dotnet publish -o output
Then execute the compilation of the Java code with the following:
javac -classpath .\output\JCOBridge.jar JavaUseExample.java -d .\output
The previous compiles the Java class and write it into the output folder. Then run the following:
java -classpath .\output\JCOBridge.jar;.\output JavaUseExample
and you will get the output fully generated from the .NET runtime:
Hello, World!, From .NET
Just a tip: if you installed a .NET version different from 8, an error is reported. To overcome that error update the command line adding the CLR version to be used:
java -classpath .\output\JCOBridge.jar;.\output JavaUseExample --CoreCLRVersion:9


