我最终自己解决了这个问题。它没有涉及我最初的预感,所以我更改了这个问题的标题以更好地反映实际问题。
虽然 jni4net 包有很多有用的示例,但似乎没有任何好的示例显示对象在没有一些奇怪的构建体操的情况下双向传递(参见原始问题陈述)。我想出了如何做到这一点,并在此处介绍了解决方案以及构建结果所需的命令。
运行此程序的最简单方法是按照说明设置 jni4net,并将所有这些内容放入 jni4net 软件包随附的示例目录的新子目录中。
首先是 C# 代码:
src/test/left.cs
namespace test
{
using System;
using net.sf.jni4net;
using test;
public class left : iright
{
public static void Main(String[] args)
{
left l = new left();
l.sendMeToRight();
}
public left()
{
Console.WriteLine("left side constructed...");
}
public void sendMeToRight()
{
BridgeSetup bridgeSetup = new BridgeSetup();
bridgeSetup.AddAllJarsClassPath(".");
Bridge.CreateJVM(bridgeSetup);
Bridge.RegisterAssembly(typeof(right).Assembly);
Console.WriteLine("Sending myself to right.");
right il = new right(this);
il.announceMyself();
}
public void announceMyself()
{
Console.WriteLine("Hello from the left side...");
}
}
}
现在是 Java 代码:
src/test/iright.java
package test;
public interface iright
{
void announceMyself();
}
src/test/right.java
package test;
public class right implements iright
{
public right(iright f)
{
System.out.println("right side constructed... ");
f.announceMyself();
}
public void announceMyself()
{
System.out.println("Hello from the right side...");
}
}
最后是构建脚本(我构建它以在 cygwin 中运行,并且在很短的时间内通知,因此根据需要进行更改。):
@echo off
rm -rf build
mkdir build
mkdir build\test
copy ..\..\lib\*.* build
echo Compile base classes.
javac src/test/*.java
mv src/test/*.class build/test
echo Creating jar file.
jar cvf build\lr.jar -C build test/iright.class -C build test/right.class
rm build/test/*.class
echo Generating proxies.
..\..\bin\proxygen.exe build\lr.jar -wd build
echo Compiling derived proxy classes.
javac -cp build\lr.jar;build\jni4net.j-0.8.0.0.jar build\jvm\test\iright_.java build\jvm\test\right_.java
echo Packing compiled derived proxy classes.
jar cvf build\lr.j4n.jar -C build\jvm test\__iright.class -C build\jvm test\iright_.class -C build\jvm test\right_.class
echo Generating derived proxy DLL.
cd build
csc.exe /nologo /warn:0 /t:library /out:lr.j4n.dll /recurse:clr\*.cs /reference:"c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll" /reference:jni4net.n-0.8.0.0.dll
echo Generating product executable.
csc.exe /nologo /warn:0 /out:demo.exe /target:exe /reference:jni4net.n-0.8.0.0.dll /reference:lr.j4n.dll ..\src\test\left.cs