Mac under Java JNI (Java Native Interface) c

Source: Internet
Author: User

1. Writing a Java class with a native declaration method

2. Compiling Java classes using the Javac command

3. Use the Java-jni className to generate a header file with the suffix named. h

4. Implementing local methods using other languages (c, C + +)

5. Generate a dynamic-link library of files written by local methods

One code: (Do not take the package path)

/**
* Native-0study
*
* @author Xuedui.zhao
* @create 2018-04-23
*/
public class HelloWorld {
public native void Hello ();

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

public static void Main (string[] args) {

New HelloWorld (). hello ();
}
}

Two:

In terminal execution: Javac Helloworld.java

The Helloworld.class file is generated

Three: Use Java-jni className to generate header file with suffix. h

/* 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

IV: Create a HELLOWORLDIMPL.C file

#include "jni.h"
#include "HelloWorld.h"
#include <stdio.h>
Jniexport void Jnicall Java_helloworld_hello (jnienv *env,jobject obj) {
printf ("Hello world!\n");
Return
}

V: Generate a dynamic-link library of files written by local methods

Gcc-dynamiclib-i/library/java/javavirtualmachines/jdk1.8.0_162.jdk/contents/home/include Helloworldimpl.c-o Libhello.jnilib
An exception is thrown at this point:

Helloworldimpl.c:1:10:fatal error: ' jni.h ' File not found
#include "jni.h"
^~~~~~~
1 Error generated.

VI: sudo cp/library/java/javavirtualmachines/jdk1.8.0_162.jdk/contents/home/include/darwin/jni_md.h/library/java/ Javavirtualmachines/jdk1.8.0_162.jdk/contents/home/include

Seven: Ls-al

Drwxr-xr-x 7 Xuedui.zhao Staff 224 4 23 21:35.

Drwxr-xr-x 9 Xuedui.zhao Staff 288 4 23 11:52..

-rw-r--r--1 Xuedui.zhao Staff 442 4 14:07 Helloworld.class
-rw-r--r--1 Xuedui.zhao Staff 377 4 14:07 HelloWorld.h
-rw-r--r--1 Xuedui.zhao Staff 276 4 14:06 Helloworld.java
-rw-r--r--1 Xuedui.zhao Staff 4 21:27 HELLOWORLDIMPL.C
-rwxr-xr-x 1 Xuedui.zhao Staff 8400 4 21:35 libhello.jnilib

Eight: Terminal execution: Java HelloWorld

Hello world!

Mac under Java JNI (Java Native Interface) c

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.