JNI4Net C# To Java

2019-07-03 14:23发布

问题:

I have a simple Java class called jniBridge.Calculator that has one simple method Add(int a, int b). After building the project using eclipse, i exported the project as a JAR file.

I then called proxygen on the JAR file, this produced a folder called clr and another folder called jvm that contained both the C# and Java proxies respectively. Proxygen also created a build.cmd and an .xml files as well.

After that i ran the build.cmd it produced a .DLL and .JAR file.

Now i want to use these or whatever in C#, so i copied the .DLL and .JAR files into the .NET project folder and added a reference to the .DLL file and set the .JAR file to Copy Always to the output folder so that it exists along side the .EXE file. I also added a reference to the Jni4Net main library file jni4net.n-0.8.6.0.dll and copied its main JAR file, jni4net.j-0.8.6.0.jar, to the same directory. Adding to the dump, i also added the original JAR file i started with.

Inside the C# Program.cs i do the following:

BridgeSetup bs = new BridgeSetup();
bs.AddAllJarsClassPath(".");

Bridge.CreateJVM(bs);
Bridge.RegisterAssembly(typeof(jniBridge.Calculator).Assembly);

However, the last invocation fails with the following exception:

Unhandled Exception: net.sf.jni4net.jni.JNIException: Can't load java class for jnibridge.Calculator from classLoader sun.misc.Launcher$AppClassLoader@e39a3e ---> java.lang.ClassNotFoundException: jnibridge.Calculator

Any help please!!

回答1:

I had this issue, and fixed it setting the package name in lowercase in my Java code (and also the reference to the namespace in your C# harness). In your case, you might try and confirm the package name is (exactly) jnibridge (not JniBridge or anything else).