English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
When developing in Java, we often encounter many local libraries. This forces us to face a choice when packaging the project: either to place the library files together with the packaged jar file; or to include the library files in the jar.
Packaging a small project into a jar has many publishing advantages, and this time I will share a method of packaging JNI into a jar.
[Implementation Approach]
After packaging the JNI library (dll, so, etc.) into a jar, we cannot access them through the path, and the library reading depends on an external library file with the corresponding name under java.library.path. We only need to release it from the jar package before calling JNI, which is similar to the file copy process.
[Selection of Deployment Location]
java.library.path is not a fixed location, and the following code can be used to print it out:
System.out.println(System.getProperty("java.library.path"));
For example, on my computer, the result is as follows:
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;;.
The absolute path may vary due to different systems, so the relative path "." is chosen here, which is the project directory.
[JNI Deployment Class]
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>(); //Here add the local library file name, or you can also slightly modify it to read an external xml or properties file 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; } try{ File f = new File("."); + File.separator + sourceName); if(!f.exists()){ f.createNewFile(); System.out.println("[JNIDEV]:DEFAULT JNI INITIALIZE:");+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("Local library loaded successfully"); } catch(UnsatisfiedLinkError e){ System.out.println("Local library loading failed"); } } }
After that, we place the local library in the same package as the class:
[Running Result]
[JNIDEV]: DEFAULT JNI INITIALIZATION: luajava-1.1.dll
[JNIDEV]: DEFAULT JNI INITIALIZATION: libluajava-1.1.so
Local library loaded successfully
Summary
That's all about the content of this article on a brief introduction to packaging JNI libraries into jar files. I hope it will be helpful to everyone. Those who are interested can refer to other Java-related topics on this site. Welcome to leave a message if there is anything insufficient. Thank you for your support to this site!
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been edited by humans, and does not assume any relevant legal responsibility. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report violations, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)