从C#调用我自己的Java代码 [英] Call my own Java code from C#

查看:443
本文介绍了从C#调用我自己的Java代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

拥有自己的Java代码我正在使用C#调用一些非托管代码来调用(通过JNI)java代码。我正在使用JNI,因为我需要确保:

Having my own Java code I'm using C# to call some unmanaged code that call (via JNI) the java code. I'm using JNI since I need to ensure:


  • Java代码在真实JVM上运行而不是在某些.NET上运行的能力VM

  • 连接到VM进行调试的能力(IKVM不支持)

  • 我需要免费解决方案

  • 目前的免费解决方案不适用(例如IKVM)

  • the ability that the Java code will run over real JVM and not over some .NET VM
  • the ability to attach to the VM for debugging (IKVM does'nt support it)
  • I need free solution
  • The current free solutions are not applicable (e.g. IKVM)

无论如何,我的问题是如何管理传递的字符串在这些层之间以最佳方式没有泄漏。
我正在做类似的事情:

Anyway, my question is how can I manage strings passed between these layers in the best manner without leaks. I'm doing something like:

[DllImport(@"MyDll.dll")]
public extern static void receive_message(string receDest, StringBuilder response);

这意味着我正在为托管代码中的响应分配内存。
我想避免这种情况,因为我事先并不知道响应长度。如何编写JNI适当的方法,为托管代码分配正确的缓冲区而不会泄漏。 JNI代码应该是线程安全的。

This means I'm allocating the memory for the response in the managed code. I want to avoid that since I don't know in advance the response length. How can I write a JNI appropriate method that will allocate the right buffer for the managed code without leaks. The JNI code should be thread safe.

有任何建议吗?

谢谢,

Guy

推荐答案

可能需要JNI,但您的要求不是我真的指出了它。

You may need JNI, but your requirements don't really indicate it.

使用真正的JVM 的要求并没有规定使用JNI。我建议提高你的要求,或考虑更松散的耦合。例如,套接字通信,Web服务,共享数据库,共享文件或队列

The requirement to use a real JVM does not dictate the use of JNI. I'd suggest sharpening your requirements, or considering looser coupling. For example, socket comms, web services, a shared database, a shared file, or a queue.

如果你真的需要Java和.NET在相同的过程中运行,并且紧耦合,请考虑 JNBridge

他们已经解决了你所面临的问题。

If you really need Java and .NET to be run in the same process, with tight coupling, consider JNBridge.
They've solved the problem you are confronting.

这篇关于从C#调用我自己的Java代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆