/Java2CSharp

Execute C# .net code from Java code

Primary LanguageC++

JavaCallCSharp

Java call C# lib build with .NET CORE 2.0 via C++ as wraper The code is based on example from coreCLR

Java using JNI to call C++ code. then C++ host a core CLR to run C# code.

graph LR
Jaava[Java]  --> B((C++ .cpp))
B --> C((Core CLR))
C --> D[.NET core 2.2]
Loading

pre-require

.NET CORE SDK 2.0: I only tested it in Ubuntu 18.04 x64 (Follow https://dotnet.microsoft.com/download/linux-package-manager/ubuntu18-04/sdk-current)

If you already have recent version of donet need to remove it

sudo apt remove dotnet*

Install .NET Core 2.1 SDK (works well if you dont have any dotnet installed, tested Ubuntu 20.04.3 LTS wsl)

  1. Enable Microsoft PPA
wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb 
sudo dpkg -i packages-microsoft-prod.deb
  1. Installing .NET Core 2.1 SDK
sudo apt update 
sudo apt install apt-transport-https 
sudo apt install dotnet-sdk-2.1 
  1. Installing build tools (gcc 6: std C++ 14 for the filesystem)
sudo apt-get update
sudo apt-get install build-essential
sudo apt install cmake
  1. Installing Java
sudo apt install default-jre
sudo apt install default-jdk

usage:

git init
git submodule update

dotnet build Managed.csproj

cmake .
cmake --build . 

javac Sample1.java
java -cp sample1.jar Sample1 

You should keep the compiled file in the same folder.