Getting started
These steps are an initial guide to the JCOBridge NuGet package. The same package is used from .NET and from Java/JVM, but the setup always starts from .NET.
Mandatory tools
You need .NET on the development machine, preferably the current LTS (.NET 10). Follow the Microsoft instructions if needed.
You also need a JDK of your choice: follow the vendor instructions. To keep this guide simple, set the JAVA_HOME environment variable, either as the vendor describes or manually.
Create your first JCOBridge project
Open a shell in a location of your choice and create a console project:
mkdir MyFirstProject
cd MyFirstProject
dotnet new console
dotnet run
You will see the expected output:
Hello, World!
Add the JCOBridge package:
dotnet add package MASES.JCOBridge
This installs the latest version. To install a specific version, for example 2.6.10:
dotnet add package MASES.JCOBridge --version 2.6.10
Open Program.cs and replace its content with the following, a reviewed version of the 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();
}
}
}
}
Run it again with dotnet run and you get the same output, this time generated by the JVM:
Hello, World!
Tip
This step needs the JAVA_HOME environment variable described above.
Start from the JVM
To use JCOBridge from the JVM you first need some Java code. 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;
}
}
}
Publish the project to create a subfolder with the needed libraries:
dotnet publish -o output
Compile the Java class into the output folder.
On Windows:
javac -classpath .\output\JCOBridge.jar JavaUseExample.java -d .\output
On Linux and macOS:
javac -classpath ./output/JCOBridge.jar JavaUseExample.java -d ./output
Then run it.
On Windows:
java -classpath .\output\JCOBridge.jar;.\output JavaUseExample
On Linux and macOS:
java -classpath ./output/JCOBridge.jar:./output JavaUseExample
The output is now generated by the .NET runtime:
Hello, World!, From .NET
Tip
If the installed .NET version is not the one JCOBridge selects by default, an error is reported. Add the CLR version to use to the command line, for example --CoreCLRVersion:10.