JNI Issue

We are new to JNI and facing a problem while integrating an SDK written in "C" with our web application.

Below is the problem description:

We are having a problem to call JNI method through Java Web Application.

We have followed the approach of JNI ( Java Native Interface) to bridge a communication with C programs. As JNI accepts only DLL approach to communicate with required C functions. We have created DLL from C code by using CL command available in VC++. Now in our Java application we have sucessfully created and loaded the DLL, but when we call the JNI method through DLL we are getting following error.

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet execution threw an exception

root cause

java.lang.UnsatisfiedLinkError

:  Method name

package_name. classname.MethodName(Native Method) .

We are not able to solve this issue.

We think that it is packaging issue because the DLL is not created as per the package given in Java Class.

We don't understand why we are getting HTTP Status 500 error while calling JNI method.
Experts, please guide us; If you aware about this problem.

p.s. - I am going to add relevant code segments if required.
InteractiveBrainAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

VenabiliCommented:
java.lang.UnsatisfiedLinkError - that's your problem.
Make sure that the dll is exported as part of the java.library.path or that you load it from the code.
0
cupCommented:
When you built your C interface, was it built as C or as C++.  Can you check the compile options: it should be /TC not /TP.  Under Properties/C++/Advanced/Compile As, make sure it says compile as C code.
0
InteractiveBrainAuthor Commented:
Thanks you Venabili and cup for helping to resolve this. I have verified both approaches however the error encounters only when we atempt to call this class from web application.

We have written a simple java class as given below and trying to call this class in our web application. We have placed the “jniTest.dll” in “C:\windows\system32” and the same path has been set in classpath. We are able to run in command prompt but when we tried to do the same with web application we are getting the below error.

java.lang.UnsatisfiedLinkError: sayHello
      at com.fedex.application.business.jniTest.sayHello(Native Method)
      at com.fedex.application.business. jniTest.main(ExtractAstra.java:13)
Exception in thread "main"
jniTest.java
package com.fedex.application.business;
public class jniTest{
                static {
                                System.loadLibrary("jniTest");
               }
public native String sayHello(String s);
public static void main(String[] argv)        
{                              
                                jniTest nt = new jniTest();
                                retval = nt.sayHello("Welcome ");
}
}

jniTest.c

#include "jniTest.h"        /*double quotes tells it to search current directory*/
JNIEXPORT jstring JNICALL Java_com_fedex_application_business_jniTest_sayHello (JNIEnv *env, jobject thisobject, jstring js)
{              
 Printf(“%s , JNI test is successful”, js);
}
0
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

VenabiliCommented:
>We have placed the “jniTest.dll” in “C:\windows\system32” and the same path has been set in classpath.

That's native code, you do not set it in the classpath, you set it into the java.library.path or LD_LIBRARY_PATH.
What OS are yoiu running this on? (so I can give you syntax for it)
0
InteractiveBrainAuthor Commented:
Venabili: - the OS is Windows XP Prof. SP2
0
VenabiliCommented:
then use
set  LD_LIBRARY_PATH=c:\windows\system32
(if this is where the DLL is)
Or use
java -Djava.library.path=c:\windows\system32 YourProgram
when you start it.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
InteractiveBrainAuthor Commented:
Venabili: Thanks for your valuable assistance. It worked. We also had figured a pacakge level issue that was restricting us to create DLL...here's how I resolved it....

1.Compile the java file(command: javac com/company/application/business/nativetest.java)
2.Create the header file(command: javah.exe -jni -o nativetest.h com.company.application.business.nativetest)
3.Create the DLL file(Command: cl -I C:\j2sdk1.4.2_03/include -I C:\j2sdk1.4.2_03/include/win32 -LD nativetest.c tcAutoTest.c ...all other C files goes here.... vTools.c -nativetest.dll)
4.Set the Java lib path to the newly created DLL file. (Venabili: This is where your guideline worked)
5.Build the project
6.Copy the java class file in server shared  classes folder.
7.Delete the java class from build/webinf/classes folders.
8.Run the application.

..and it worked!
0
VenabiliCommented:
Great - yep, sounds like what I usually do more or less.

Good luck with your JNI :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.