• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java native interface under servlet

 
chandu joshi
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to add jni code into my servlet,this is right way.If yes tell me a book or tutorial for basic guideline.
[ October 15, 2005: Message edited by: chandu joshi ]
 
dema rogatkin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are familiar with C, then steps are quite simple:
1. create a class which should use native methods and mark some methods as native;
2. compile your class and then use javah.exe to generate C headres;
3. Implement methods in C using headers from 2;
4. add in Java code loading of .dll or .so before calling native methods
5. Note that all memory you allocated and passed to Java (for example your method returns a string) won't be collected unless you use some specific actions

I'd recommend to get JNI manual from Sun's web site, I used it and created several native implementations with great success.

As an example, you can look in SWT implementation. There is nothing specific for servlets, unless your servlet container uses security manager for limiting responsibilities of servlets. In this case you should give your servlet permission to load native libraries.

There is a small problem for serlet packaging using native method, because commonly loadlib method looks for a native library in system path. You can use load method to specify exact location of a library. I think you can figure out a location of deployed war content and provide correct path to a native library.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by chandu joshi:
[QB]I want to add jni code into my servlet,this is right way.If yes tell me a book or tutorial for basic guideline.


By the way, why you want to do this?
I would say you should not, otherwise your application would be no more platform independent.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic