Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HowTo: Call C# static functions in a .Net Core 3.1 DLL from Java #750

Closed
PhilippPaetzold opened this issue Nov 24, 2020 · 2 comments
Closed
Assignees
Labels
question Item is a user question, and should not involve a coding change

Comments

@PhilippPaetzold
Copy link

Hello,

I recently had to port my windows application from .NET Framework 4 to .NET Core (3.1) and part of the application is a Java frontend (for historical reasons) which performs interop with the C# API through a wrapper DLL with some static functions in it. For Java/C# interop is have been using jni4net, but this is not working with .NET Core anymore.

I was hoping that I could use xamarin/java.interop for that. I have cloned the repo and can build it successfully. I also have been looking at the source code examples / unit tests. But I can not figure out how to get it working for my use case.

I have a couple of questions:

  • Are there special requirements for the Java side? For example, is a specific JDK/JNI Version required?
  • Do I need to change Java code at all, when using this framework? I import my C# static functions with the native keyword...
  • Does java.interop generate the bridge jni interface between Java and C# (I assume this will be a native C++ DLL). How do I use source code generation for this?
  • Do I have to change the C# side? I saw an attribute for exporting methods in the source code. Is this the way to go?

It would be nice, if somebody can point me in the right direction.

Thank you and best regards
Philipp

@jpobst
Copy link
Contributor

jpobst commented Nov 24, 2020

Java.Interop is largely a specialized case allowing interop between Mono's C# and Android's Java. Although there are probably some pieces that someone could take to build a more general solution on top of, this is not something that you can just drop into your project today.

I think you will be better off pursuing other options to achieve your goal.

@jpobst jpobst self-assigned this Nov 24, 2020
@jpobst jpobst added the question Item is a user question, and should not involve a coding change label Nov 24, 2020
@jpobst jpobst closed this as completed Dec 7, 2020
@jonpryor
Copy link
Member

jonpryor commented Dec 7, 2020

@PhilippPaetzold: What you want is possible, but not straightforward.

What exists now requires:

  • macOS
  • Mono
  • Mono be responsible for creating the JVM, via JreRuntime

…all of which is a non-starter for you.

(Why Mono? Because only Mono has the supported needed for our GC bridge.)

What you want is an inversion, initializing the .NET runtime from Java, along with .NET Core support.

The inversion exists, but only for Android.

.NET Core support will require at least a way to disable the GC bridge, e.g. #426

I would love to have this support in xamarin/java.interop, but this support does not currently exist.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Item is a user question, and should not involve a coding change
Projects
None yet
Development

No branches or pull requests

3 participants