Simple use of JNI in JAVA

Source: Internet
Author: User

Package com. home; public class Testdll {// declare the name of the called database static {System. loadLibrary ("hello");} // make a local Declaration on the method to be called. The native keyword must be public native static int get (); public native static void set (int I); public static void main (String [] args) {Testdll test = new Testdll (); test. set (5); System. out. println (test. get ());}}


Program call to generate the required library file.

Note: first go to the src directory and then execute

/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class com_home_Testdll */#ifndef _Included_com_home_Testdll#define _Included_com_home_Testdll#ifdef __cplusplusextern "C" {#endif/* * Class:     com_home_Testdll * Method:    get * Signature: ()I */JNIEXPORT jint JNICALL Java_com_home_Testdll_get  (JNIEnv *, jclass);/* * Class:     com_home_Testdll * Method:    set * Signature: (I)V */JNIEXPORT void JNICALL Java_com_home_Testdll_set  (JNIEnv *, jclass, jint);#ifdef __cplusplus}#endif#endif

 

JNIEXPORT jint JNICALL Java_com_home_Testdll_get (JNIEnv *, jclass );

 #include "com_home_Testdll.h"    int i = 0;    JNIEXPORT jint JNICALL Java_com_home_Testdll_get (JNIEnv *, jclass)    {    return i;    }    JNIEXPORT void JNICALL Java_com_home_Testdll_set (JNIEnv *, jclass, jint)    {    i = j;    }

Related Article

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.