Mac implementation of JNI demo

Source: Internet
Author: User

Today in the ArrayList source see the System.arraycopy this method, but this method does not have Java implementation.

After a query lookup, it is known that the native is a Java call to the C language to implement the operation of JNI. And then just write

A JNI demo.

1 writing a Java file with native


public class HelloWorld {

public native void Hello ();

static{
System.loadlibrary ("Hello");
}

public static void Main (string[] args) {

New HelloWorld (). hello ();
}
}

2 command-line compilation Javac Helloworld.java

3 command-line compilation Javah-jni HelloWorld generate HelloWorld.h file

/* Don't EDIT this file-it are machine generated */
#include <jni.h>
/* Header for class HelloWorld */

#ifndef _included_helloworld
#define _included_helloworld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class:helloworld
* Method:hello
* Signature: () V
*/
jniexport void Jnicall Java_helloworld_hello
(JNIENV *, jobject);

#ifdef __cplusplus
}
#endif
#endif
4 Writing the implementation of C language HELLOWORLD.C
#include <jni.h>
#include "HelloWorld.h"
#include <stdio.h>
Jniexport void Jnicall Java_helloworld_hello (jnienv *env,jobject obj) {
printf ("Hello world!\n");
Return
}
5 writing. C implementation. o File

gcc-fpic-d_reentrant-i/library/java/javavirtualmachines/jdk1.7.0_75.jdk/contents/home/include-i/library/java/ Javavirtualmachines/jdk1.7.0_75.jdk/contents/home/include/darwin-c HELLOWORLD.C

Two addresses the first one is jni.h the second one is jni_md.h as shown.

6 Compile and build the local library file. Dylib


gcc-shared Helloworld.o-o Libhello.dylib

7 Running Java files Helloword

All of the above files are best placed together to allow permission.

Mac implementation of JNI demo

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.