The first of Android advanced calls C library functions in Java

Source: Internet
Author: User

The steps to invoke the C function through JNI in Java code are as follows:

First step: Writing Java code

1 classhellojni{2     native voidPrinthello ();3     native voidprintstring (String str);4     5     Static{system.loadlibrary ("Hellojni");}6     7      Public Static voidMain (String args[]) {8Hellojni Myjni =NewHellojni ();9         Ten Myjni.printhello (); OneMyjni.printstring ("Hello World from C Fun"); A          -     } -      the}

Step two: Compiling Java code

Javac Hellojni.java generating Hellojni.class files

Step three: Generate the C language header file

Javah Hellojni generating HelloJNI.h header files

/*Do not EDIT this file-it are machine generated*/#include<jni.h>/*Header for Class Hellojni*/#ifndef _included_hellojni#define _included_hellojni#ifdef __cplusplusexternC{#endif/** Class:hellojni * Method:printhello * Signature: () V*/Jniexportvoidjnicall Java_hellojni_printhello (jnienv*, jobject);/** Class:hellojni * method:printstring * Signature: (ljava/lang/string;) V*/Jniexportvoidjnicall java_hellojni_printstring (jnienv*, Jobject, jstring); #ifdef __cplusplus} #endif #endif

Fourth step: Writing C code

Note that the C code must be written in the same way as the previous header file, which differs from the normal C function.

1#include"HelloJNI.h"2#include <stdio.h>3 4JniexportvoidJnicall Java_hellojni_printhello (JNIENV *env, Jobject obj) {5printf"Hello world!\n");6     return;7 }8 9JniexportvoidJnicall java_hellojni_printstring (jnienv * env, jobject obj, jstringstring) {Ten     Const Char*str = (*env)->getstringutfchars (env,string,0); Oneprintf"%s!\n", str); A     return; -}

Fifth step: Generate C Shared Library

Codeblocks under:

File-new-project-shared library-go

Because there is such a line in HelloJNI.h <jni.h>, the standard library file does not have this file, but this file can be found under the JDK installation directory

In order to be able to compile successfully, it is necessary to copy the corresponding files to the C compiler's header file repository, as follows:

Compile build C shared library file:

However, codeblocks default generated files prefixed with LIB, need to be removed manually, but the operation is still a problem:

1D:\hixin\c>Java hellojni2ExceptioninchThread"Main"JAVA.LANG.UNSATISFIEDLINKERROR:D:\HIXIN\C\HELLOJNI.D3Ll:can't load IA 32-bit. dll on a AMD 64-bit platform4 At java.lang.classloader$nativelibrary.load (Native Method)5 At java.lang.ClassLoader.loadLibrary0 (Unknown Source)6 At java.lang.ClassLoader.loadLibrary (Unknown Source)7 At java.lang.Runtime.loadLibrary0 (Unknown Source)8 At java.lang.System.loadLibrary (Unknown Source)9At Hellojni.<clinit> (Hellojni.java:5)

The main idea is that the generated DLL is not 64-bit, see the latest version of the online vs seems to be able to generate 64-bit DLLs, it is not tangled this

The first of Android advanced calls C library functions in Java

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.