Using JNI in a Servlet  
Author Message
steves-un





PostPosted: 2005-5-31 2:32:00 Top

java-programmer, Using JNI in a Servlet Does anyone have experience with using JNI in a servlet under Windows
XP? When trying to do this, it continually fails to load the C
library.
It works when done in a normal Java program, but not when it's a
servlet.
I've seen that others have used JNI in Servlets and had this problem
with Windows, but no explanation on how to work around the problem.

I also have problems running javah. It's not required, but it does
work
as a precursor; if javah doesn't work, the library won't load either.

Here's a quick rundown of the setup and errors. I'ms using Java 1.5
with the Sun App Server on a Windows XP SP2 system, compiling the C
code
with gcc v3.3.3. CLASSPATH is set to inculde all directories the files
and libs reside.

When running the servlet, the following errors show up in the App
Server
log file:

Logger: javax.enterprise.system.stream.err
Name-Value Pairs: _ThreadID=29;
Message ID: Link error
Complete Message java.lang.UnsatisfiedLinkError: no jni_if in
java.library.path

Logger: javax.enterprise.system.stream.err
Name-Value Pairs: _ThreadID=12;
Message ID:
Complete Message Exception in thread "Thread-79"

Logger: javax.enterprise.system.stream.err
Name-Value Pairs: _ThreadID=30;
Message ID: java.lang.UnsatisfiedLinkError
Complete Message getXppParm
at xpserv.XppIf.getXppParm(Native Method)
at xpserv.XppIf.run(XppIf.java:61)


Running javah gets the following error:

bash-2.05b$ javah -jni XppIf
error: cannot access XppIf
bad class file: C:\BioSpring\xpserv\XppIf.class
class file contains wrong class: xpserv.XppIf
Please remove or make sure it appears in the correct subdirectory
of the classpath.
com.sun.tools.javac.util.Abort
:

Running javah with the package name included in the name still fails:

bash-2.05b$ javah -jni xpserv.XppIf
error: cannot access xpserv.XppIf
file xpserv\XppIf.class not found
javadoc: error - Class xpserv.XppIf not found.
Error: No classes were specified on the command line. Try -help.

If anybody has had any success in using JNI with a servlet, I would to
hear how they configured it to work.

 
Gordon Beaton





PostPosted: 2005-5-31 15:30:00 Top

java-programmer >> Using JNI in a Servlet On 30 May 2005 11:32:25 -0700, email***@***.com wrote:
> If anybody has had any success in using JNI with a servlet, I would
> to hear how they configured it to work.

The first step is to get javah working properly (and this has nothing
to do with servlets)...

> Running javah gets the following error:
>
> bash-2.05b$ javah -jni XppIf
> error: cannot access XppIf
> bad class file: C:\BioSpring\xpserv\XppIf.class
> class file contains wrong class: xpserv.XppIf
> Please remove or make sure it appears in the correct subdirectory
> of the classpath.
> com.sun.tools.javac.util.Abort
> :
>
> Running javah with the package name included in the name still fails:
>
> bash-2.05b$ javah -jni xpserv.XppIf
> error: cannot access xpserv.XppIf
> file xpserv\XppIf.class not found
> javadoc: error - Class xpserv.XppIf not found.
> Error: No classes were specified on the command line. Try -help.

You *must* use the package name when you run javah, or the generated
symbol names will be incorrect and you will get UnsatisfiedLinkErrors
when you try to invoke them.

The second example fails because you need C:\BioSpring in the
classpath, not C:\BioSpring\xpserv.

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Alan Krueger





PostPosted: 2005-6-1 12:06:00 Top

java-programmer >> Using JNI in a Servlet email***@***.com wrote:
> Does anyone have experience with using JNI in a servlet under Windows
> XP? When trying to do this, it continually fails to load the C
> library.
> It works when done in a normal Java program, but not when it's a
> servlet.
> I've seen that others have used JNI in Servlets and had this problem
> with Windows, but no explanation on how to work around the problem.

A JNI library can only be loaded once per JVM instance under Windows.
Subsequent attempts will result in an UnsatisfiedLinkError, IIRC.
You'll need to ensure it only ever gets loaded once.

Under Tomcat, IIRC, place the enclosing jar file in common/lib or the
class file in common/classes.