JNI4Net java in c# , JNIException

2.5k Views Asked by At

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.8.0.dll and copied its main JAR file, jni4net.j-0.8.8.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:


    static void Main(string[] args)
    {
        var bridgeSetup = new BridgeSetup();
        bridgeSetup.Verbose = true;
        bridgeSetup.AddAllJarsClassPath("./");
        bridgeSetup.IgnoreJavaHome = true;
        //bridgeSetup.AddAllJarsClassPath(@"C:\Program Files\Java\jre7");
        Bridge.CreateJVM(bridgeSetup);
        Bridge.RegisterAssembly(typeof(DemoCalc).Assembly);
        ICalc calc = new DemoCalc();
        int result = calc.MySuperSmartFunctionIDontHaveInJava("Answer to the Ultimate Question of Life, the Universe, and Everything");
        Console.WriteLine("Answer to the Ultimate Question is : " + result);
    }

However, the last invocation fails with the following exception:

net.sf.jni4net.jni.JNIException HResult=-2146233088 Message=Can't load java class for democalc.DemoCalc from classLoader sun.misc.Launcher$AppClassLoader@20eb607d Source=jni4net.n-0.8.8.0 StackTrace: 在 net.sf.jni4net.utils.Registry.LoadClass(String name, ClassLoader classLoader, JNIEnv env) 在 net.sf.jni4net.utils.Registry.RegisterClass(RegistryRecord record, ClassLoader classLoader, JNIEnv env)

Any help please!!

1

There are 1 best solutions below

0
On

You need to make sure the Java package name is correct. In the question you mentioned your class is called jniBridge.Calculator, and later in the exception it seems the proxies are trying to instanciate a class called democalc.DemoCalc, and in the C# harness class you have this line of code:

Bridge.RegisterAssembly(typeof(DemoCalc).Assembly);

Make sure you name the Java package correctly (using just lowercase, as mentioned in this other question), and use the same package and class name in your C# harness. If the package is called jnibridge, and the class name is DemoCalc, the C# code should be:

Bridge.RegisterAssembly(typeof(jnibridge.DemoCalc).Assembly);