Java调用DLL
2016-05-12 13:06
417 查看
java调用dll的方法实例:
1、使用JNI调用:参考
http://wenku.baidu.com/view/927abf92f242336c1fb95e31.html
http://blog.csdn.net/whustyle/article/details/49123933
直接在java中输出类的配置:http://www.oschina.net/question/1402563_133543?fromerr=f7RcQNZn
2、使用JNA调用
例如:
需要导入jna.jar包
(1)接口类:
package com.lp.jna;
import com.sun.jna.Library;
import com.sun.jna.Native;
public interface JnaDllInterFace extends Library{
//void
//加载Dll
//JnaDllInterFace dllInterFace = (JnaDllInterFace)
//Native.loadLibrary((Platform.isWindows() ? "UnZipTool.dll" : "c"), JnaDllInterFace.class);
JnaDllInterFace instanceDll = (JnaDllInterFace)Native.loadLibrary("UnZipTool.dll",JnaDllInterFace.class);
//声明Dll中的函数,如果Dll中有个printf的函数,在此声明一个同名的
//且参数类型一致的方法,java与c的类型映射见第三节
public void getExportPatch(String strSourceZipPath, String strDestZipPath, String strNewDestZipPath);
}
(2)测试
package com.lp.test;
import com.lp.jna.JnaDllInterFace;
import com.sun.jna.Library;
public class TestDLL {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//fun("G:\\zipCeshi1\\Config.zip","G:\\zipCeshi2\\Config.zip","G:\\zipCeshi3");
//方法一:Jna调用DLL
/**
* jna测试
* 1、添加jna的jar包
* 2、添加接口extends Library
* 3、调用方法
*/
JnaDllInterFace.instanceDll.getExportPatch("G:\\zipCeshi1\\Config.zip", "G:\\zipCeshi2\\Config.zip","G:\\zipCeshi3");
System.out.println("hello world");
}
}
3、使用JNative调用
导入JNative.jar包
(1)接口文件
package com.lp.dll;
import org.xvolks.jnative.exceptions.NativeException;
public interface DllUtilTools {
void ImplementDll(String strSourcePath,String strDestPath,String strSavePath) throws NativeException, IllegalAccessException;
}
(2)实现接口文件
package com.lp.dll;
import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.exceptions.NativeException;
public class DllUtilToolsImpl implements DllUtilTools{
static JNative myjnative = null;
public void ImplementDll(String strSourcePath,String strDestPath,String strSavePath) throws NativeException,IllegalAccessException {
try {
if (myjnative == null) {
myjnative = new JNative("UnZipTool.dll", "getExportPatch");
myjnative.setRetVal(Type.INT);
}
//传入参数
//myjnative.setParameter(0,"F:\\ziptest\\ceshi1\\aaa\\aaa.zip
");
//myjnative.setParameter(1, "F:\\ziptest\\ceshi2\\v\\bbb.zip
");
//myjnative.setParameter(2, "F:\\ziptest\\ceshi3");
myjnative.setParameter(0, strSourcePath);
myjnative.setParameter(1, strDestPath);
myjnative.setParameter(2, strSavePath);
myjnative.invoke();
myjnative.getRetVal();
System.out.println(myjnative.getRetVal());
//return myjnative.getRetValAsInt();
} finally {
if (myjnative != null) {
myjnative.dispose();
}
}
}
}
(3)测试
DllUtilToolsImpl impl = new DllUtilToolsImpl();
impl.ImplementDll("F:\\ziptest\\ceshi1\\aaa\\aaa.zip
", "F:\\ziptest\\ceshi2\\v\\bbb.zip
", "F:\\ziptest\\ceshi3");
System.out.println("hello world!");
1、使用JNI调用:参考
http://wenku.baidu.com/view/927abf92f242336c1fb95e31.html
http://blog.csdn.net/whustyle/article/details/49123933
直接在java中输出类的配置:http://www.oschina.net/question/1402563_133543?fromerr=f7RcQNZn
2、使用JNA调用
例如:
需要导入jna.jar包
(1)接口类:
package com.lp.jna;
import com.sun.jna.Library;
import com.sun.jna.Native;
public interface JnaDllInterFace extends Library{
//void
//加载Dll
//JnaDllInterFace dllInterFace = (JnaDllInterFace)
//Native.loadLibrary((Platform.isWindows() ? "UnZipTool.dll" : "c"), JnaDllInterFace.class);
JnaDllInterFace instanceDll = (JnaDllInterFace)Native.loadLibrary("UnZipTool.dll",JnaDllInterFace.class);
//声明Dll中的函数,如果Dll中有个printf的函数,在此声明一个同名的
//且参数类型一致的方法,java与c的类型映射见第三节
public void getExportPatch(String strSourceZipPath, String strDestZipPath, String strNewDestZipPath);
}
(2)测试
package com.lp.test;
import com.lp.jna.JnaDllInterFace;
import com.sun.jna.Library;
public class TestDLL {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//fun("G:\\zipCeshi1\\Config.zip","G:\\zipCeshi2\\Config.zip","G:\\zipCeshi3");
//方法一:Jna调用DLL
/**
* jna测试
* 1、添加jna的jar包
* 2、添加接口extends Library
* 3、调用方法
*/
JnaDllInterFace.instanceDll.getExportPatch("G:\\zipCeshi1\\Config.zip", "G:\\zipCeshi2\\Config.zip","G:\\zipCeshi3");
System.out.println("hello world");
}
}
3、使用JNative调用
导入JNative.jar包
(1)接口文件
package com.lp.dll;
import org.xvolks.jnative.exceptions.NativeException;
public interface DllUtilTools {
void ImplementDll(String strSourcePath,String strDestPath,String strSavePath) throws NativeException, IllegalAccessException;
}
(2)实现接口文件
package com.lp.dll;
import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.exceptions.NativeException;
public class DllUtilToolsImpl implements DllUtilTools{
static JNative myjnative = null;
public void ImplementDll(String strSourcePath,String strDestPath,String strSavePath) throws NativeException,IllegalAccessException {
try {
if (myjnative == null) {
myjnative = new JNative("UnZipTool.dll", "getExportPatch");
myjnative.setRetVal(Type.INT);
}
//传入参数
//myjnative.setParameter(0,"F:\\ziptest\\ceshi1\\aaa\\aaa.zip
");
//myjnative.setParameter(1, "F:\\ziptest\\ceshi2\\v\\bbb.zip
");
//myjnative.setParameter(2, "F:\\ziptest\\ceshi3");
myjnative.setParameter(0, strSourcePath);
myjnative.setParameter(1, strDestPath);
myjnative.setParameter(2, strSavePath);
myjnative.invoke();
myjnative.getRetVal();
System.out.println(myjnative.getRetVal());
//return myjnative.getRetValAsInt();
} finally {
if (myjnative != null) {
myjnative.dispose();
}
}
}
}
(3)测试
DllUtilToolsImpl impl = new DllUtilToolsImpl();
impl.ImplementDll("F:\\ziptest\\ceshi1\\aaa\\aaa.zip
", "F:\\ziptest\\ceshi2\\v\\bbb.zip
", "F:\\ziptest\\ceshi3");
System.out.println("hello world!");
相关文章推荐
- 几种java调用dll的方式
- java 调用 dll 文件
- JavaWeb项目中调用dll动态链接库
- Java SpringMVC实现国际化整合案例分析(i18n)
- 深入浅出: Java回调机制(异步)
- Spring Security 学习笔记-授权控制过滤器
- 实时 Java 社区论坛:Sym
- Spring IOC(依赖注入,getBean)
- Java 性能调优指南之 Java 集合概览
- Java 性能调优指南之 Java 集合概览
- Java 的 JSON 开源类库选择比较
- 【DAY3】JAVA面向对象和函数
- Java多线程---从线程中返回数据
- 深入Spring:自定义IOC
- Java中HashMap遍历的两种方式
- SpringMVC表单提交Action的路径问题
- java如何在函数中调用主函数的数组
- Spring4+Hibernate4+Mybatis Maven的pom配置
- java如何在函数中调用主函数的数组
- JAVA在eclipse中配置Opencv