I was able to run the following java application("jni1.java") of Java
Native Interface
successfully according to
http://java.sun.com/nav/read/Tutorial/native1.1/stepbystep/index.html.
Instead of jni1.java, I want to use servlet("jniserv.java"), but I can
not run.
I had the following message;
jniserv: init
java.lang.UnsatisfiedLinkError: displayHelloWorld
at jniserv.service(jniserv.java:10)
at java.servlet.http.HttpServlet.service(HttpServlet.java:50)
at
sun.servlet.http.HttpServerHandler.sendResponse(HttpServerHandler.java:143)
at
sun.servlet.http.HttpServerHandler.handleConnection(HttpServerHandler.java:110)
at
sun.servlet.http.HttpServerHandler.run(HttpServerHandler.java:87)
at java.lang.Thread.run(Thread.java:481)
of course, I put hello.dll into correct path.
My questions are;
Can I call native method from servlet?
Any tips for JNI in servlet?
thanks.
--------------------------------------------------------------------------------------------
Java Application (stepbystep)
jni1.java
class jni1 {
public static void main(String[] args) {
new HelloWorld().displayHelloWorld();
}
}
HelloWorld.java
class HelloWorld {
public native void displayHelloWorld();
static {
System.loadLibrary("hello");
}
}
HelloWorld.h (generated by javah)
#include <jni.h>
#include "HelloWorld.h"
#include <stdio.h>
JNIEXPORT void JNICALL
Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj)
{
printf("Hello world!\n");
return;
}
HelloWorldImp.c
#include <jni.h>
#include "HelloWorld.h"
#include <stdio.h>
JNIEXPORT void JNICALL
Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj)
{
printf("Hello world!\n");
return;
}
to create .dll;
cl -Ic:\java\include -Ic:\java\include\win32 -LD HelloWorldImp.c
-Fehello.dll
to run;
java jni1
Hello world!
==> OK
Servlet environment
using srun included in jsdk
srun -d c:\jdk1.1\src -r c]\inetpub\wwwroot
jniserv.java
// Licensed Materials -- Property of IBM
import java.servlet.*;
import java.io.*;
import java.servlet.http.*;
public class jniserv extends HttpServlet {
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
new HelloWorld().displayHelloWorld();
}
}
compile is OK.
at Netscape,
http://xx.xxx.ibm.com:8080/servlet/jniserv
at srun console message, you will see
jniserv: init
java.lang.UnsatisfiedLinkError: displayHelloWorld
at jniserv.service(jniserv.java:10)
at java.servlet.http.HttpServlet.service(HttpServlet.java:50)
at
sun.servlet.http.HttpServerHandler.sendResponse(HttpServerHandler.java:143)
at
sun.servlet.http.HttpServerHandler.handleConnection(HttpServerHandler.java:110)
at
sun.servlet.http.HttpServerHandler.run(HttpServerHandler.java:87)
at java.lang.Thread.run(Thread.java:481)
--
// Yasu Suzuki
// e-mail: suz...@yamato.ibm.co.jp