Java bean with JNI

java-native-interfacejavabeansjsptomcat

I have added native method in java bean code.

Then i have copied .dll file in System32 folder.

while using javabean in JSP.

it gives an Error:

HTTP Status 500 –

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.UnsatisfiedLinkError: BeanDir.mysimplebean.sayHello()Ljava/lang/String;
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:522)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:398)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

javax.servlet.ServletException: java.lang.UnsatisfiedLinkError: BeanDir.mysimplebean.sayHello()Ljava/lang/String;
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:862)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)
    org.apache.jsp.jsp.callbean_jsp._jspService(callbean_jsp.java:124)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

java.lang.UnsatisfiedLinkError: BeanDir.mysimplebean.sayHello()Ljava/lang/String;
    BeanDir.mysimplebean.sayHello(Native Method)
    org.apache.jsp.jsp.callbean_jsp._jspService(callbean_jsp.java:112)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

note The full stack trace of the root cause is available in the Apache Tomcat/6.0.20 logs.
Apache Tomcat/6.0.20

Java bean code:

package BeanDir;
import java.util.*;
//This file must be compiled Manually using javac 
//cd D:\tomcat\webapps\examples\WEB-INF\classes\BeanDir\mysimplebean.java
public class mysimplebean 
{
    static
    {
        String s=System.getProperty("java.library.path");
        System.setProperty("java.library.path",s);
        System.loadLibrary("HelloWorld");
    }
    public String getceoname() 
    {   
        String ceonameval = "Tom Hanks CEO of Tom Hanks INC";        
        return ceonameval;
    }  
    public String ceoemail() 
    {   
        String ceoemailval = "tom@tomhanksinc.com";      
        return ceoemailval;
    } 
    public double findtakehome(int salary,String designation) 
    {           
        double takehomeamt;
        if(designation=="Developer")
        {
            takehomeamt = salary+salary*0.15; //15 % Raise in Salary
        }
        else
        {
            takehomeamt = salary+salary*0.10; //10 % Raise in Salary
        }
        return takehomeamt;
    }
    public native String sayHello();    
}

Best Answer

I cant leave a comment, but..

  1. is your servlet container on the same windows machine that the dll is on?
  2. does the System.getProperty("java.library.path") include the system32 folder (from within a jsp)?

the answer to both questions should be 'yes' for this to work

Related Topic