tags:

views:

210

answers:

5

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:

  • 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);

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.

Any suggestions?

Thanks,

Guy

+6  A: 

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

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.

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.

Cheeso
1. JNBridge is not free.2. I didn't understand how can I configure the JVM.3. In their samples you need to load the JVM by your own (calling the java directly)Thanks,Guy
Guy
I understand JNI is not free. Didn't see that as a requirement. How much is your time worth?
Cheeso
+1 for the suggestion to keep it simple by using sockets to communicate between the two processes.
Mark
+2  A: 

You might be interested in trying to convert your Java bytecode code in .NET CIL with IKVM.NET.

lmsasu
This is not an option since:1. I need the java code to be in java and I need to debug it.2. I don't want to run over VM other than "real" JVM (not IKVM)Thanks,Guy
Guy
+1  A: 

You essentially need to make a remote call into the java program from your .NET-code.

With your current skillset I would suggest that you create a web service in the Java machine - this is relatively easy in Java 6 - and based on the WSDL create a client in your .NET program.

This is probably the cleanest solution with todays technologies.

If that for some reason isn't good enough, then add to your question.

Thorbjørn Ravn Andersen
A: 

I think you could use jni4net as a bridge library. Or you could just look at source code and grab some ideas (LGPL/GPL).

Pavel Savara
A: 

Guy -- Regarding Cheeso's response, and your respones to it:

  • JNBridgePro does allow the JVM to be started automatically and run inside the .NET process (in addition to the option of starting it explicitly). See the "shared-memory" communications mechanism discussed in the documentation.

  • JNBridgePro does allow you to attach a Java debugger, even when the JVM is running inside the .NET process. Contact [email protected] for details, as well as for details on configuring the JVM.

  • Can't do much about it not being free, but it might be worth your while to check it out anyway.

Disclosure: Yes, I am with JNBridge.

Wayne Citrin