Solved

Servlets and JNI

Posted on 2001-06-08
9
532 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

749 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