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?
 
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
 
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.