当前位置: 移动技术网 > IT编程>开发语言>Java > 浅谈将JNI库打包入jar文件

浅谈将JNI库打包入jar文件

2019年07月19日  | 移动技术网IT编程  | 我要评论

在java开发时,我们有时候会接触到很多本地库,这样在对项目打包的时候我们不得不面临一个选择:要么将库文件与包好的jar文件放在一起;要么将库文件包入jar。

将一个不大的项目包成一个jar有诸多发布优势,本次将分享一个将jni包入jar的方法。

[实现思路]

将jni库(dll、so等)包入jar后,我们无法通过路径来访问它们,而库的读取依赖一个java.library.path下对应名称的外部库文件,我们仅仅需要在调用jni前将其由jar包释放出来,这类似于文件的拷贝过程。

[部署位置的选取]

java.library.path并不是一个固定的位置,使用下面代码可以打印出来:

system.out.println(system.getproperty("java.library.path"));

例如在本人的计算机上,结果是这样的:

d:\program files (x86)\java\jre7\bin;c:\windows\sun\java\bin;c:\windows\system32;c:\windows;d:/programfiles (x86)/java/jre7/bin/client;d:/program files(x86)/java/jre7/bin;d:/program files (x86)/java/jre7/lib/i386;c:\program files(x86)\nvidia corporation\physx\common;c:\windows\system32;c:\windows;c:\windows\system32\wbem;c:\windows\system32\windowspowershell\v1.0\;e:\develop\jdk1.7.0_71\bin;e:\develop\git\cmd;e:\develop\git\bin;e:\develop\apache-maven-3.2.1\bin;e:\eclipse-java-luna-sr1-win32\eclipse;;.

绝对路径会由于不同系统而变化,因而这里选择了”.”这个相对路径,也就是项目目录下。

[jni部署类]

import java.io.file;
import java.io.fileoutputstream;
import java.io.inputstream;
import java.util.arrays;
import java.util.linkedlist;
import java.util.list;
public class jnidevelopment {
	byte[] cache;
	list<string> sources;
	public jnidevelopment(){
		cache = new byte[1024];
		sources = new linkedlist<string>();
		//这里加入本地库文件名,也可以稍加修改读取一个外部xml或properties 
		sources.add("luajava-1.1.dll");
		sources.add("libluajava-1.1.so");
	}
	private boolean sourceexist(string sourcename){
		file f = new file("." + file.separator + sourcename);
		return f.exists();
	}
	public void dodefaultdevelopment(){
		for (string s:sources){
			dodevelopment(s);
		}
	}
	public boolean dodevelopment(string sourcename){
		if(sourceexist(sourcename)){
			return true;
		} else{
			try{
				file f = new file("." + file.separator + sourcename);
				if(!f.exists()){
					f.createnewfile();
					system.out.println("[jnidev]:default jni inition:"+sourcename);
				}
				fileoutputstream os = new fileoutputstream(f);
				inputstream is = getclass().getresourceasstream(sourcename);
				if(is == null){
					os.close();
					return false;
				}
				arrays.fill(cache,(byte)0);
				int realread = is.read(cache);
				while(realread != -1){
					os.write(cache, 0, realread);
					realread = is.read(cache);
				}
				os.close();
			}
			catch(exception e){
				system.out.println("[jnidev]:error in copy jni lib!");
				e.printstacktrace();
				return false;
			}
		}
		return true;
	}
	public static void main(string[] args) {
		jnidevelopment deve = new jnidevelopment();
		deve.dodefaultdevelopment();
		try{
			system.loadlibrary("luajava-1.1");
			system.out.println("本地库加载成功");
		}
		catch(unsatisfiedlinkerror e){
			system.out.println("本地库加载失败");
		}
	}
}

之后我们将本地库放置在与该类同包下:

[运行结果]

[jnidev]:default jni inition:luajava-1.1.dll
[jnidev]:default jni inition:libluajava-1.1.so
本地库加载成功

总结

以上就是本文关于浅谈将jni库打包入jar文件的全部内容,希望对大家有所帮助。感兴趣的朋友可以参阅本站其他java相关专题。如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网