MyEclipse+Tomcat环境下Java调用DLL问题
windows xp系统,在MyEclipse下做一个Java调用dll的程序
在src下面编写javabean代码:
javacall.java
public class javacall {
static
{
System.loadLibrary("calldll");
}
public native static int add(int i, int k); //在calldll.dll实现
public static void main(String[] args)
{
javacall jc = new javacall();
System.out.println(jc.add(3,4));
}
}
编译生成javacall.class文件,生成javacall.h文件
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
在VC6.0下生成dll文件,程序如下
#include <jni.h>
#include "javacall.h"
JNIEXPORT jint JNICALL Java_javacall_add (JNIEnv *, jclass, jint a,jint b)
{
return a*b;
}
在命令行下直接运行java javacall结果正确
如果这时候直接在MyEclipse下run application会报错:
java.lang.UnsatisfiedLinkError: no calldll in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at javacall.<clinit>(javacall.java:4)
Exception in thread "main"
这时在run-java application-arguments-VM arguments下面添加
-Djava.library.path=E:\MyEclipse\workspace\jspcall\WebRoot\WEB-INF\classes
这个路径是calldll.dll的存放路径,也是javacall.class所在路径
然后运行成功,得到想要的结果12
最后我在WebRoot下面编写call.jsp文件
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
Jsp调用DLL例子
</title>
</head>
<jsp:useBean id="JBean" scope="page" class="javacall" />
<body>
<h1>
<%=JBean.add(3,4)%>
</h1>
</body>
</html>
配置到tomcat6.0下,
工程结构大致为jspcall在C:\Tomcat 6.0\webapps
call.jsp在C:\Tomcat 6.0\webapps\jspcall
javacall.class跟calldll.dll都在
C:\Tomcat 6.0\webapps\jspcall\WEB-INF\classes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
然后输入http://localhost:8080/jspcall/call.jsp
出现错误:
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: Unable to compile class for JSP:
An error occurred at line: 8 in the jsp file: /call.jsp
javacall
cannot be resolved to a type
5: Jsp调用DLL例子
6: </title>
7: </head>
8: <jsp:useBean id="JBean" scope="page" class="javacall" />
9: <body>
10: <h1>
11: <%=JBean.add(3,4)%>
An