0

I have stuck in the class->header file for couple days!

I have tried on jni on Client by http://netbeans.org/kb/docs/cnd/beginning-jni-linux.html and http://ringlord.com/jni-howto.html. And it succeeded in return "hello JNI C++" from JNI's (.cpp)file. Here are my steps:

  1. create native function and in client.java
  2. clean &build this client.java on Netbeans IDE, then result a client.class file
  3. javah –jni [package].[classname]
  4. create a dynamic library C++ project as first reference does, and put client.h into source file, and put some hello code into (.cpp)file ---> It works!

However, I tried to do the same thing on the servlet side and it's not working

  1. Servlet.java->Servlet.class : ok!
  2. Servlet.class->Servlet.h: fail!!!! Error : cannot access javax.servlet.GenericServlet class file for javax.servlet.GenericServlet not found

The following are solutions I have found and tried so far,

  • check the package name

  • sudo gedit /etc/profile,sudo gedit .bashrc, sudo /etc/environment; add JAVA_HOME & CLASSPATH on them, and source them to update, then echo $JAVA_HOME, echo $CLASSPATH to verify

  • download servlet-api-6.0.14.jar & servlet-api-5.0.16.jar from http://www.jarfinder.com/index.php/java/info/javax.servlet.GenericServlet ,and add above two (.jar) by netbeans IDE->server->property->libraries->Add JAR

Please tell me how to figure it out this issue, thank you very much!!Btw, I am using hessianServlet

子昂 陳
  • 111
  • 1
  • 2
  • 7
  • It is not a good idea to bundle your Servlet code in native class. Any specific reason to do so ? – Mohsin Oct 31 '12 at 12:31
  • I need to upload image to servlet, then use the c++ code in server side to do processing by openCV. So I need JNI native class on servlet in order to do – 子昂 陳 Oct 31 '12 at 13:09
  • I guess that the class which declares native methods is deriving from javax.servlet.GenericServlet. Is there any reason why you need to do that? – Pavel Zdenek Oct 31 '12 at 13:32
  • Send the image from your Servlet to a simple java class method which calls a native method to do the processing. Compile the java class and generate the header file to use in your native code. – Mohsin Oct 31 '12 at 13:38
  • @Mohsin , so far I am only trying to return "hello world" from cpp to servlet. for the image path part, i have already uploaded image from client to server by heesian protocol; – 子昂 陳 Oct 31 '12 at 13:44
  • @Pravel Zdenek , the reason why I use JNI on servlet is to provide other developers who feel more comforable in c/c++ coding with openCV – 子昂 陳 Oct 31 '12 at 13:45

1 Answers1

2

NativeWrapper.java (you run javah only on this class)

class NativeWrapper {
  // either
  static {
    System.loadLibrary("MyOpenCVNative");
  }
  // or
  public NativeWrapper() {
    System.loadLibrary("MyOpenCVNative");
  }
  public native void callNative();
}

MyServlet.java

class MyServlet extends javax.servlet.GenericServlet {
  private NativeWrapper nativeWrapper = new NativeWrapper();

  public void someServletMethod() {
    nativeWrapper.callNative();
  }
}
Pavel Zdenek
  • 7,146
  • 1
  • 23
  • 38
  • com.caucho.hessian.client.HessianConnectionException: 500: java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:8080/example_ws_server/servlet/hwsexm – 子昂 陳 Nov 02 '12 at 06:52
  • hi, @Pavel Zdenek, the above error message is what I got after following your tutorial. – 子昂 陳 Nov 02 '12 at 06:53
  • Also, I have tried another method without using extra class like above NativeWrapper, then failed! private native void nativeServletPrint(); public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{ new ExampleServiceServlet().nativeServletPrint(); } It runs ok return the processing result from servlet to client, but after i do javah on this servlet.class. no header file stopped my progress. thx – 子昂 陳 Nov 02 '12 at 07:00
  • Don't mix your servlet class with the native methods. Keep it separated as posted in the answer. I can't help you with the IOException, i don't know Hessian. Post a new question, it's not directly related to JNI. – Pavel Zdenek Nov 02 '12 at 09:23