2013-02-27 47 views
0

我想构建一个简单的java servlet在我的机器上的tomcat服务器上运行。NoClassDefFoundError尝试加载与tomcat的servlet中的dll

我的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 
    } 

} 

,它工作正常

我也有一个加载DLL中的第二类(不是calldll callingdll = new calldll();部分我会在下面我得到了什么的错误解释)文件名为“calldll.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; 
} 

当我运行在那里我调用加载DLL我得到这个调用DLL类javaservlet:

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类

赶上(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:■

+0

添加的servlet .jar添加到你的类路径中。希望它有效。如果你使用的是Eclipse,你可以添加为外部库尝试。 – 2013-02-27 09:49:38

+0

感谢您的重播,您可以更具体地了解我必须做什么? – user878813 2013-02-27 09:50:33

+0

我相信你在用'System.loadLibrary(“calldll”);'得到错误。捕获'loadLibrary'方法抛出的异常。这将有助于找出错误的确切原因。 – 2013-02-27 09:52:16

回答

0

如果你的webapp在Tomcat中的应用程序服务器托管的dll复制到bin文件夹,然后在你的servlet使用:

System.load(System.getProperty("catalina.base")+"/bin/yourdll.dll");