JNI Combat in Eclipse

Source: Internet
Author: User
Tags stub

1. What is JNI?

JNI is an important feature in the Java standard platform, which makes up for Java's lack of platform-agnostic, and can interact with dynamic libraries in other languages such as C and C + +, while Java is implemented across platforms, giving other languages an opportunity to take advantage. With the support of the Java standard platform, the JNI pattern is easier to implement and use

Here is no longer to repeat, attached to others summed up the figure:

650) this.width=650; "style=" width:1101px; "src=" http://pic002.cnblogs.com/images/2011/269610/2011061500160640. PNG "alt=" 2011061500160640.png "/>

Original link: http://www.cnblogs.com/mandroid/archive/2011/06/15/2081093.html

Knowledge Supplement:

Stub class, which implements an interface, but every method that is implemented is empty.

Its role is: If an interface has many methods, if you want to implement this interface, it is necessary to implement all the methods. But a class may require only one or two of these methods in terms of business. If you implement this interface directly, all other unrelated methods are implemented, in addition to implementing the required methods. If you implement an interface by inheriting the stub class, you are relieved of the hassle.


Header file

Baidu explained: Generally will be used to include the file extension called. h, called the header file. Content that is common to #include文件的目的就是把多个编译单元 (that is, C or cpp files) is placed in a single file to reduce overall code size, or to provide cross-engineering common code.


2 Javah

Using Javah, you can obtain your Java source files and generate A/C + + header file that contains the JNI stubs (stub,c header files) for all local methods (native methods) in your Java code.


3.eclipse Configuration Javah


the authoring steps for JNI

A. Java class for writing methods with native declarations

B. Compile the class implemented in a with the Javac command

C, Javah-jni Java class name generates a header file with the extension h

D. Implementing a Local method using C + +

E. Generate a dynamic-link library of local methods in D



Start by creating a new class that contains the local method (native method), and thenative identifier implies that the methods are implemented, except that the implementations are non-Java and the code is as follows:

/** * * */package com.magc.jni;            /** * @author MAGC * * */public class HelloWorld {static {system.loadlibrary ("Hello");    } public native void Displayhello (); /** * @param args */public static void main (string[] args) {new HelloWorld ().    Displayhello (); }}

Note:

load and loadLibrary Differences

    1. They can all be used to load library files, whether they are JNI library files or non-JNI library files. The corresponding JNI library file must be loaded with one of the two methods before any local methods are called.

      The 2.system.load parameter is the absolute path to the library file, which can be any path.
      For example, you can load a JNI library file in a Windows platform like this:
      System.load ("C:\\Documents and Settings\\testjni.dll");

      3. The system.loadlibrary parameter is the library file name and does not contain the extension of the library files.
      For example, you can load a JNI library file in a Windows platform like this
      System. LoadLibrary ("Testjni");

      Here, TestJNI.dll must be in the path that the JVM variable points to java.library.path.
      You can get the value of the variable in the following ways:
      System.getproperty ("Java.library.path");
      By default, under the Windows platform, this value contains the following location:
      1) Some directories related to the JRE
      2) Program Current directory
      3) Windows directory
      4) system directory (SYSTEM32)
      5) The System environment variable path specifies the directory.

The difference between classpath and Java.library.path

classpath Path, can only be jar or class file, no will error, because they will be load into the JVM


To find a shared library for a Java program or to specify Java.library.pathwhen executing a java program, use Eclipse to set the following:

    1. properties->run/debug settings->arguments->VM Arguments

    2. -----------------------------------------

    3. -djava.library.path=/home/miaoyachun/workspace/jnic/release



You need to use an external tool, next to the Java program button we run, or through the Run option on the menu bar to enter the External tool and select the External tool configuration to create a new startup item HelloWorld

The parameters are as follows:

Location:

D:\program Files\java\jdk1.7.0_51\bin\javah.exe

Working Directory

${project_loc}

Arguments:

-v-classpath "${project_loc}/bin"-D "${project_loc}/jni" ${java_type_name} (here v means verbose output is enabled)

650) this.width=650; "title=" capture. PNG "src=" http://s3.51cto.com/wyfs02/M02/6C/EF/wKioL1VX-gOAjSc3AAPzvMwruUE028.jpg "alt=" Wkiol1vx-goajsc3aapzvmwruue028.jpg "/>



4. Generate the Implementation function header file (extension h)

To run the tool, the cursor must be positioned in the Java source file where the header file needs to be generated, otherwise it will be reported as ${project_loc} This variable is an empty error

650) this.width=650; "title=" capture. PNG "src=" http://s3.51cto.com/wyfs02/M01/6C/F4/wKiom1VX-hTh4B9_AAEW0lN78Nc665.jpg "alt=" wkiom1vx-hth4b9_ Aaew0ln78nc665.jpg "/>



A com_magc_jni_helloworld.h header file is generated under the JNI directory of the current project, which is used by C, C + + programs to reference and implement functions in it

/* Don't EDIT this file-it are machine generated */#include <jni.h>/* Header for class Com_magc_jni_helloworld */# ifndef _included_com_magc_jni_helloworld#define _included_com_magc_jni_helloworld#ifdef __cplusplusextern "C" {# endif/* * Class:com_magc_jni_helloworld * Method:displayhello * Signature: () V */jniexport void Jnicall java_com_ Magc_jni_helloworld_displayhello (jnienv *, jobject); #ifdef __cplusplus} #endif #endif

Note: 1), this header file is not required to compile the user, directly for other C, C + + program reference.

2), the Java_com_magc_jni_helloworld_displayhello (jnienv *, Jobject) method in this header file , is the interface that will interact with the dynamic link library in the future, and need names to be consistent.



5. generate a dynamic-link library (with the extension "so" under windows with Ddl,linux) using the C + + Implementation Local method :


#include <jni.h> #include "com_magc_jni_helloworld.h" #include <stdio.h>jniexport void Jnicall java_com_    Magc_jni_helloworld_displayhello (jnienv *env, Jobject obj) {printf ("from Jni_helloworldimpl.cpp:"); printf ("Hello World!    \ n "); return;}

This C + + file implements the functions in the header file above, noting that the method function names should be consistent.


Compiled to generate dynamic link library files, with a concrete implementation of the dynamic library, copied to the Project Bin directory, you can run Java calls the JNI program class native method


This article is from the "dream belongs to the brave-Skywalker" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1652034

JNI Combat in Eclipse

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.