JAVA动态加载dll任意路径加载dll

2 篇文章 0 订阅
package com.ctl.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.StringTokenizer;

public class SayHellotoCPP {
	//LIBFILENAME dll文件的路径
	static final String LIBFILENAME = "D:\\inca_workspace\\JNI\\bin\\HelloEnd.dll";// dll 文件
	static {
		try {
			// 获取到java.library.path  及系统变量中Path中的内容
			String libpath = System.getProperty("java.library.path");
			if (libpath == null || libpath.length() == 0) {
				throw new RuntimeException("java.library.path is null");
			}
			//javaBinPath   jdk的bin目录D:\Program Files\Java\jdk1.6.0_11\bin
			String javaBinPath = null;
			StringTokenizer st = new StringTokenizer(libpath,
					System.getProperty("path.separator"));
			if (st.hasMoreElements()) {
				javaBinPath = st.nextToken();
			} else {
				throw new RuntimeException("can not split library path:"
						+ libpath);
			}
			// 把dll文件写入到java.library.path中该dll放在ConvertWord2HM相同目录下,这个可以是你的类名
			InputStream inputStream = SayHellotoCPP.class.getResourceAsStream(
					LIBFILENAME);
			final File dllFile = new File(LIBFILENAME);
			if (!dllFile.exists()) {
				FileOutputStream outputStream = new FileOutputStream(dllFile);
				byte[] array = new byte[1024];
				int bytesRead = -1;
				while ((bytesRead = inputStream.read(array)) != -1) {
					outputStream.write(array, 0, bytesRead);
				}
				outputStream.flush();
				outputStream.close();
			}
			// 动态加载dll
			System.load(dllFile.getPath());
			// 在虚拟机关闭的时候删除dll
			dllFile.deleteOnExit();
		} catch (Throwable e) {
			throw new RuntimeException("load Convert.dll error!", e);
		}

	}


	public SayHellotoCPP() {
	}

	public native void sayHello(String name);

	public static void main(String[] args) {		
		SayHellotoCPP shp = new SayHellotoCPP();
		shp.sayHello("World");
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值