Solved

Servlets and JNI

Posted on 2001-06-08
9
531 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!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

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.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
This video teaches viewers about errors in exception handling.

860 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