2013-02-27 30 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"); 
相關問題