尝试使用tomcat在servlet中加载dll时出现NoClassDefFoundError

时间:2013-02-27 09:38:02

标签: java tomcat servlets dll noclassdeffounderror

我正在尝试构建一个在我的机器上的tomcat服务器上运行的简单java servlet。

我的servlet代码是:

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class javaservlet
 */
@WebServlet(description = "java servlet", urlPatterns = { "/javaservlet" })
public class javaservlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    /**
     * @see HttpServlet#HttpServlet()
     */
    public javaservlet() {
        super();

        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        PrintWriter writer = response.getWriter();
        calldll callingdll = new calldll();
        ServletContext context = getServletConfig().getServletContext();  
        String path = context.getContextPath(); 
        writer.println(path);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

并且它工作正常(不是calldll callingdll = new calldll();部分我将在下面解释我到达那里的错误)

我还有第二个类加载名为“calldll.dll”的dll文件(使用javac,javah和东西完成所有工作并且它有效)我的dll放在C:\apache-tomcat-7.0.37\wtpwebapps\myServlet\WEB-INF\lib中我指向了构建原生路径和我的班级代码是

public class calldll {

    private native void print();

public static void main (String[] args){
    new calldll().print();
}

static {
System.loadLibrary("calldll");  
}

}

我从中制作dll的c文件非常简单

#include<jni.h>
#include<stdio.h>
#include<windows.h>
#include "calldll.h"

JNIEXPORT void JNICALL
Java_calldll_print(JNIEnv*env,jobject obj)
{
printf("It Works!");
return;
}

当我运行javaservlet时,我调用了加载dll的调用dll类,我得到了这个:

java.lang.NoClassDefFoundError: Could not initialize class calldll
    javaservlet.doGet(javaservlet.java:33)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

奇怪的是,当我单独运行java类时,会出现“It works”消息,因此类成功加载了dll。但是,当我在servlet中创建一个类的实例时,它不是来自servlet,这不是我的问题....

我加入了calldll类

catch(UnsatisfiedLinkError e)  {      System.err.println(“本机代码库无法加载。\ n”+ e);  }

查看静态是否存在问题,运行servlet时出现以下错误

Native code library failed to load.
java.lang.UnsatisfiedLinkError: no calldll in java.library.path

但我的calldll类,如果我单独执行它仍然有效...所以在我做错了的servlet中发生了什么:s

1 个答案:

答案 0 :(得分:0)

如果您的webapp托管在tomcat app server中,请将dll复制到bin文件夹,然后在您的servlet中使用:

{{1}}