Linux platform Java call so library-jni use example

Source: Internet
Author: User

Transferred from: http://blog.chinaunix.net/uid-20180960-id-1972669.html

Linux platform Java call so library-jni use example 2010-08-11 22:17

1. Ensure that the GCC compiler is installed

2. Write the Hellojni.java code and declare the functions that need to be implemented in C with native.
If the source program is included in the package, the same folder structure should be created, such as/home/swan/test/net/wangliping/hellojni.java

Package net.wangliping

public class Hellojni
{
Static
{

System.loadlibrary ("Goodluck");
}

public native static int get ();
Public native static void set (int i);

public static void Main (string[] args)
{
Hellojni test = new Hellojni ();
Test.set (10);
System.out.println (Test.get ());
}
}

3. Compile the. java file in the same directory as the Hellojni.java file.

Javac Hellojni.java

4. Compile the. class file generated in the third step under ~/desktop, generate the corresponding. h header file, and the header file generated in this example is named Net_wangliping_hellojni.h

Javah Net.wangliping.HelloJNI

5. Write the hellojni.c file to implement the. h header file that was generated in the 4th step and implement the method declared therein.

#include "net_wangliping_hellojni.h"

int i = 0;

Jniexport jint jnicall java_net_wangliping_hellojni_get (jnienv *env, Jclass JC)
{
return i;
}

Jniexport void Jnicall Java_net_wangliping_hellojni_set (jnienv *env, Jclass JC, Jint J)
{
i = j;
}

6. Compile the Hellojni.c file in the 5th step into an. o file

gcc-fpic-d_reentrant-i/usr/lib/jvm/java-1.5.0-sun-1.5.0.19/include-i/usr/lib/jvm/java-1.5.0-sun-1.5.0.19/ Include/linux-c HELLOJNI.C

Note:/usr/lib/jvm/java-1.5.0-sun-1.5.0.19/include is the path where the jni.h header file resides
/usr/lib/jvm/java-1.5.0-sun-1.5.0.19/include/linux is the path where Jni_md.h is located.

7. Compile the HELLOJNI.O file generated in step 6th into a. So library file

gcc-shared Hellojni.o-o libgoodluck.so

Note: Unlike Windows, the Linux library file must be in libxxx.so form (or libxxx.so.y,y is the version number), the LIB prefix is for the system to recognize it, XXX is the Java code system.loadlibrary (" xxx "); the name of the referenced library.

8. Copy the libgoodluck.so file generated in step 7th to the path that the Java load Library Ld_library_path points to.
The premise is that you have sufficient permissions if you do not have sudo permission for the classmate, use the following method:
1) Create a directory under/home to store the. So library files, such as/home/swan/lib
2) Copy the libgoodluck.so file to this directory
3) Open terminal, run the following command

Export Ld_library_path=/home/swan/lib

Valid only under the current shell ...

Master to Candle Supplement: The following method can directly use the absolute path to load the so library file, so do not need to do the above to place the path or change the library path action.

static {
System.load ("/home/swan/test/libgoodluck.so");
}

9. Running the Hellojni.class

Java Net.wangliping.HelloJNI

When everything is OK, you can see that the result of the run is output of 10, whose value is implemented and returned by the C language code.

Linux platform Java call so library-jni use example

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.