【发布时间】:2013-02-27 09:38:01
【问题描述】:
我正在尝试构建一个在我机器上的 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 类时,会出现“它可以工作”的消息,因此该类成功加载了 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
【问题讨论】:
-
将 servlet.jar 添加到您的类路径中。希望它有效。如果您使用的是 eclipse,您可以添加为外部库尝试。
-
感谢重播,您能更具体地说明我需要做什么吗?
-
我相信您在使用
System.loadLibrary("calldll");时遇到了错误。捕获loadLibrary方法抛出的异常。这将有助于找到错误的确切原因。 -
我确实添加了你的建议,我编辑了我的答案,请检查:)
-
您检查过 java.library.path 以确保 calldll 确实存在吗?
标签: java tomcat servlets dll noclassdeffounderror