Loading-.net-DLL-in-Java
This repository includes CSharp(.cs), C, Java files.Here the C# DLL is loaded into Java using NativeAOT feature of .net 7. The C# DLL is first loaded into C code ,and the DLL from C code is used in Java using JNI concept. Native AOT(Ahead Of Time)compilation can be used with both Windows and Linux platforms.
Steps:
-
Create a C# class library either in Visual Studio or Visual Studio Code
-
Annote the functions to be exported with UnmanagedCallersOnlyAttribute.
-
Publish the C# code,
Static Library: dotnet publish /p:NativeLib=Static -r win-x64 -c release //For Linux -: linux-64 Dynamic Library: dotnet publish -r win-x64 -c Releaserelease //For Linux -: linux-64
-
Construct a C code calling the exported functions from C#.
-
Construct a Java code with call to native functions(C functions).
-
Compile the Java Code.
javac -h . filename.java
-
Include the generated headers in the C code .
-
Compile C code to create DLL.
-
Run the Java Program.