Solved

Servlets and JNI

Posted on 2001-06-08
9
530 Views
Last Modified: 2013-11-24
I am having a lot of trouble invoking functions in a Windows DLL using JNI from within a servlet.  I can load the same DLL and execute functions in it with no problem using the normal java runtime but as soon as I try to do it using a servlet under Tomcat 3.2 I get the following error:

     java.lang.UnsatisfiedLinkError: Init0

The servlet I'm attempting to use is located in the WEB-INF\classes directory as is the windows DLL.  Does anyone have any idea why this would not work?
0
Comment
Question by:derekl
  • 4
  • 3
  • 2
9 Comments
 

Expert Comment

by:smileyxx
ID: 6168962
Look at your java.library.path System property. It needs to have the directory containing your shared library.

There is a quick & easy way to do this. Copy the shared library into your jre's client library directory.

For instance on solaris, this would be:
$JAVA_HOME/jre/lib/sparc/client,

on Linux:
$JAVA_HOME/jre/lib/i386/client.

This is a relatively painless way to do it. You may try setting LD_LIBRARY_PATH to the directory containing the shared library, but often the server will set it's own LD_LIBRARY_PATH.
0
 

Author Comment

by:derekl
ID: 6168972
OK, I've got a stupid question.  What are java.library.path and LD_LIBRARY_PATH are they both environment varialbes?
0
 

Expert Comment

by:smileyxx
ID: 6169008
LD_LIBRARY_PATH is a user environment variable, java.library.path is a System property of the JVM.
(e.g.
System.out.println(System.getProperty("java.library.path"));
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:derekl
ID: 6169043
So I would have to set java.library.path to include the directory containing my dll at runtime in order to ensure everything works as planned?
0
 
LVL 3

Accepted Solution

by:
sghosh092199 earned 200 total points
ID: 6169049
LD_LIBRARY_PATH is an environment variable in UNIX Environment. The same is PATH in Windows. Its value can be returned to Java application by the following call:

String libPath = System.getProperty("java.library.path");
 
If your shared library is libname.so or libname.DLL, make sure that the path contains an entry like /usr/dir1/dir2/libname.so or /usr/dir1/dir2/libname.DLL


0
 

Author Comment

by:derekl
ID: 6169115
OK, I've got a new problem.  Although I can load the library correctly now, as soon as I try to execute a native method I get the following error:

    java.lang.UnsatisfiedLinkError: displayHelloWorld

0
 

Author Comment

by:derekl
ID: 6169128
Sorry smileyxx I meant to give the answer to you and I just clicked on the wrong comment by mistake.  Is there any way I can give you the 200 points?
0
 

Expert Comment

by:smileyxx
ID: 6169286
Don't worry about the pts.
Make sure the function you're calling is in the loaded version of the DLL. Due to a bug in the JVM, it may require a restart of the server.
0
 
LVL 3

Expert Comment

by:sghosh092199
ID: 6169361
It was too quick an evaluation though.

Say, your method signature in file RemoteServer.java is as follows:
 public native void displayHelloWorld();

'javah RemoteServer' will generate RemoteServer.h which needs to be included in DLL.

Make sure in that case the DLL implements a function as follows:
JNIEXPORT void JNICALL
Java_RemoteServer_displayHelloWorld(JNIEnv *env, jobject obj)

In case more parameters have been passed, they need to have the correct mapping. Say, int should be represented as jint.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 45
web services creation SOAP vs REST 5 41
servlet example 11 40
login form jsp example 2 25
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question