Multi-platform native library into jar package release combat

Source: Internet
Author: User

1. Preface

In developing Java applications, you will often encounter dynamic libraries or static libraries that need to be compiled using native languages such as C + +, in which case it is often necessary to publish the pre-compiled platform library files with the jar package, given the principle of brevity, We might want to get these library files into the jar to make it easier for the user to deploy, but since Java itself is not looking for the library files in the jar package correctly, this article will show you how to get the library files into the jar package with an example.

2. Preparation

The example code used in this article is built based on Gradle, so before you download the instance code, make sure that the configuration of Java and Gradle,gradle is configured correctly only by downloading the Gradle precompiled package from the official website and then configuring the bin directory to the PATH environment variable.

This article is a routine github address: Https://github.com/WeJoy/JNIJar

After the code is downloaded, execute the gradle command under the project path:

ls readme.md*  build.gradle*  src/

Esther-asus:jnijar-time5.73 secsthis build could be faster, please consider Using the Gradle Daemon:https://docs.gradle.org/2.10/userguide/gradle_daemon.html

Complete the project build, then go to the generated build/libs directory, and you can see the jar package generated by this routine compiled by Java:

 esther-asus:jnijar-master halfmang2$ cd build/libs 
Esther -asus:libs halfmang2$ ls jnijar1. 0.0 . Jar Doc/
Esther -asus:libs halfmang2$ Java-jar JNIJar1. 0.0 .jarjniutil: " /users/halfmang2/downloads/jnijar-master/build/libs/libsnapshoot.so " is loaded! helloworld !
Esther -asus:libs halfmang2$ ls jnijar1. 0.0 . Jar Doc/ libsnapshoot.so

As we can see, after executing the jar package, we will output the helloworld! generated in the native code. String, indicating the successful loading of the so library, after the jar package is executed, will automatically generate a so library in the path of the jar package, which is the jar package automatically recognize the current environment, from the jar package in the corresponding environment of the path copied out, below we will explain the next step of this routine.

3. Description

This example code is mainly composed of the following parts:

Src/main/conf/doc, the directory contains the libsnapshoot.so Library of C + + source code, you can try to compile their own

Src/main/java/com.hjess.mainapp, the main entry class for the routine

Src/main/java/com.hjess.device.snapshoot, Libsnapshoot's JNI class, which contains a byte[] getshoot () method, Get a string from the native library, also known as "helloworld!".

src/main/java/com.hjess.utils. Jarutil, a tool class that provides a way to help a Java program get the absolute path method for the currently executing jar file, and also helps to get some other methods, such as the jar package name.

src/main/java/com.hjess.utils. jniutil, the core class of this article , provides a way to judge the current JVM environment and, from the currently executing jar package, find the native library that should be the JVM environment. and copy to the jar package, and load the tool method.

src/main/resources, the resource directory, where all the content will be entered into the jar package root directory, in this case, the resources directory into the various environments of the precompiled Native Library, note that the environment directory name is dead in the code, Temporarily do not change, of course, you can also modify the Jniutil code to make some adjustments, specific methods please refer to the code (super Simple).

Build.gradle, Gradle constructs the required script, which consists primarily of a release method that copies the contents of src/main/conf to Build/libs and the Java code with src/main/ The contents of the resources are made into jar packages, generated into src/main/conf, and can also be used in some other Java scenarios, or can be packaged using Gradle plug-ins such as Shadowjar, essentially similar in principle.

4. Conclusion

Basic content is these, if you think this small project is very useful, please on the GitHub star One, in addition, because just write a small tool, did not consider a lot of adaptation situation, code is also rough, welcome to help Modify, first thanked ~ ~

Multi-platform native library into jar package release combat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.