I am using jni4net to access Java code from within a C# application, and vice-versa. jni4net uses reflection to generate JNI code proxies, so obviously one of the limitations is that your Java and C# code have to compile in order to build proxies.
Unfortunately this can result in a catch-22 problem. Consider:
C# class X uses Java class Y
Java class Y uses C# class X
Neither can be compiled, so the well established workaround is to take one of the classes (either X or Y), strip it down to its bare signature and get it to compile and then generate the proxy from the compiled skeleton. You can then replace the stripped class with the original and go on your merry way.
This seems like an ugly approach to me and I believe there should be a better way. An obvious solution would be to tell the compiler (either C# or Java, it really does not matter which) to ignore references to the missing class.
Is ignoring references to a certain missing class feasible for either the C# or Java compilers? Is there a better way to do this (and no, I am not open to considering sockets or anything of that nature; I require true interop between .NET and Java)?
Example code was requested:
Example code with jni4net bridge code removed for clarity. IA and IB are simple interfaces also not included.
Java:
public class A implements IA
{
public void m1()
{
System.out.println("m1 called");
}
public static void main (String args[])
{
IB b = new B();
b.m2(new A());
}
}
C#:
public class B : IB
{
public void m2(IA a)
{
a.m1();
A a2 = new A();
a2.m1();
}
}
I ended up solving the problem on my own. It did not involve my original hunch, so I changed the title of this problem to better reflect the actual issue.
While the jni4net package has a lot of useful examples, there did not seem to be any good ones showing object passing in both directions without some strange build gymnastics (see original problem statement). I figured out how to do it and present the solution here along with the commands required to build the result.
The easiest way to get this running is to set up jni4net as the instructions say and drop all of this stuff into a new sub-directory of the sample directory that comes with the jni4net package.
First the C# code:
src/test/left.cs
And now the Java code:
src/test/iright.java
src/test/right.java
And finally the build script (I built it to run in cygwin, and on very short notice, so alter as needed.):