我已经使用 jni4net proxygen 成功生成了从 Java 到 .NET 的代理。我现在正尝试在 Visual Studio 2017 中创建一个 .NET 解决方案,并在 .NET 控制台程序中使用 Java 代理。我已经从代理生成中导入了 .dll,它似乎可以正常工作,因为 Visual Studio 正在自动填充我的 Java 类 ( TempClientMain
) 并且没有显示任何错误。
namespace ConsoleApp1
class Program
{
static void Main()
{
var setup = new BridgeSetup(string[] args);
setup.Verbose = true;
setup.AddAllJarsClassPath("./");
Bridge.CreateJVM(setup);
Bridge.RegisterAssembly(typeof(TempClientMain).Assembly);
TempClientMain test = new TempClientMain();
test.run();
}
}
然而,当我构建并运行它时,我得到以下 cmd 输出:
loading core from C:/Users/Ruben/source/repos/ConsoleApp1/ConsoleApp1/bin/x64/Debug/jni4net.n-0.8.8.0.DLL
loading jni4net.n-0.8.8.0, Version=0.8.8.0, Culture=neutral, PublicKeyToken=134a23405600bab4 from C:\Users\Ruben\source\repos\ConsoleApp1\ConsoleApp1\bin\x64\Debug\jni4net.n-0.8.8.0.dll
loaded jni4net.n-0.8.8.0, Version=0.8.8.0, Culture=neutral, PublicKeyToken=134a23405600bab4 from C:\Users\Ruben\source\repos\ConsoleApp1\ConsoleApp1\bin\x64\Debug\jni4net.n-0.8.8.0.dll
Initialized jni4net core
core loaded from C:/Users/Ruben/source/repos/ConsoleApp1/ConsoleApp1/bin/x64/Debug/jni4net.n-0.8.8.0.DLL
clr.version :v4.0.30319
clr.arch :64bit
java.home :C:\Program Files\Java\jdk1.8.0_161
java.version :1.8.0_161
sun.arch.data.model :64
loading mediasense.j4n, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null from C:\Users\Ruben\source\repos\ConsoleApp1\ConsoleApp1\bin\x64\Debug\mediasense.j4n.dll
Unhandled Exception: net.sf.jni4net.jni.JNIException: Can't load java class for *MY TARGET PACKAGE PATH* from classLoader sun.misc.Launcher$AppClassLoader@c387f44 ---> java.lang.ClassNotFoundException: *MY TARGET PACKAGE PATH*
at net.sf.jni4net.jni.JNIEnv.ExceptionTest()
at net.sf.jni4net.jni.JNIEnv.CallObjectMethodPtr(IJvmProxy obj, MethodId methodIdNative, Value[] args)
at java.lang.ClassLoader.loadClass(String par0)
at net.sf.jni4net.utils.Registry.LoadClass(String name, ClassLoader classLoader, JNIEnv env)
--- End of inner exception stack trace ---
at net.sf.jni4net.utils.Registry.LoadClass(String name, ClassLoader classLoader, JNIEnv env)
at net.sf.jni4net.utils.Registry.RegisterClass(RegistryRecord record, ClassLoader classLoader, JNIEnv env)
at net.sf.jni4net.utils.Registry.BindJvm(RegistryRecord record, ClassLoader classLoader, JNIEnv env)
at net.sf.jni4net.utils.Registry.BindJvm(RegistryRecord record, JNIEnv env)
at net.sf.jni4net.utils.Registry.RegisterType(Type type, Boolean bindJVM, JNIEnv env)
at net.sf.jni4net.utils.Registry.RegisterAssembly(Assembly assembly, Boolean bindJVM)
at net.sf.jni4net.Bridge.RegisterAssembly(Assembly assembly)
at ConsoleApp1.Program.Main(String[] args) in C:\Users\Ruben\source\repos\ConsoleApp1\ConsoleApp1\Program.cs:line 21
Press any key to continue . . .
它加载正常,但似乎没有找到目标文件夹。在我写MY TARGET PACKAGE PATH的地方,显示了与 proxygens 目标文件夹相同的包路径(没有大写字母)。我应该把它放在哪里以便 .NET 程序可以找到它?我尝试将文件夹粘贴到项目根目录和完成的构建文件夹中。根本不包括它时,我也会收到错误消息。我对 Visual Studio 不是很熟悉,所以除了粘贴它以包含它之外,还有其他方法吗?