JNI yes Java Native Interface Abbreviation , Chinese is JAVA Local call . Use JNI It's very convenient to use our Java Program call C/C++ Program . A lot of times , Some functions use Java It can't be done , For example, some functions related to the underlying driver , Then we can use JNI To call C perhaps C++ Program to achieve , This is it. JNI The power of . however JNI It also has its disadvantages , Use java Interact with locally compiled code , Platform portability is often lost .

Here's a JNI Example , call C++ Output "hello world":

First step : establish Java class , Define a local method in it ( use native Methods of keyword modification )

public native void sayHello();

The second step : Use javah command (javah  Full path of class ) To generate local methods C++ The header file

stay DOS Enter the project directory in the window , And then execute javah com.test.TestNative command , After execution, a suffix named .h The header file , Such as com_test_TestNative.h, This header file is named according to the package name and class name .

 /* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_test_TestNative */ #ifndef _Included_com_test_TestNative
#define _Included_com_test_TestNative
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_test_TestNative
* Method: sayHello
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_test_TestNative_sayHello
(JNIEnv *, jobject); #ifdef __cplusplus
}
#endif
#endif

15、16 OK, that's right TestNative Local methods in class sayHello() Statement of . This h The document is equivalent to what we're doing in java The interface inside , Here's a statement Java_com_test_TestNative_sayHello (JNIEnv *, jobject); Method , Then we implement this method in our local method , Which means we're writing C/C++ The method name used in the program must be the same as here .

The third step : To write C/C++ Native code , Generate DLL file

First, in the VC6.0( Of course, other tools can also be used ) Create a dll engineering ---Win32 Dynamic-Link Library engineering . And then the header file generated above com_test_TestNative.h Add to the project , Then create a source file to reference the header file and implement the function of local function in the header file :

 #include<iostream.h>
#include"com_test_TestNative.h" JNIEXPORT void JNICALL Java_com_test_TestNative_sayHello(JNIEnv *env, jobject obj)
{
cout<<"hello world!"<<endl;
}

Here because com_test_TestNative.h Introduced in jni.h So will jni.h Add to VC6.0 Install under directory Include Directory .jni.h stay JDK Install under directory include in , At the same time include/win32 Two header files in jawt_md.h、jni_md.h Also import to VC6.0 in .

After importing the dependent header file , We can build the project , Press F7 That's it , It'll be in the project catalog Degug Create a DLL file under the directory , What I generate here is NativeCode.dll. We can put the dll Copy files to environment variables path Included in the catalog for us Java The program calls , For convenience , We can also put dll Add the project directory to the environment variable path In the middle , This can avoid the trouble of copying every time . Pay attention to restart after modifying environment variables myeclipse.

Step four :Java Call local function

 package com.test;
public class TestNative {
public native void sayHello(); /**
* @param args
*/
public static void main(String[] args) {
System.loadLibrary("NativeCode");
TestNative tNative = new TestNative();
tNative.sayHello();
}
}

The first 10 The line is to load the DLL ,JVM You only need to load once to call ,“NativeCode” Is the name of the DLL generated above , No suffix .

Run the program , Successfully printed out "hello world".

Java adopt JNI call C++ More articles on the program

  1. JAVA Use JNI call C++ Dynamic link library

    JAVA Use JNI call C++ Dynamic link library Use JNI Connect DLL Dynamic link library , And call the function First C++ Write the correlation function in , The file named test.cpp, Use g++ Compiled into DLL file , The instructions are as follows : g++ -sha ...

  2. Java adopt JNI call dll Detailed process ( turn )

    Source :Java adopt JNI call dll Detailed process Recently, the project has such a requirement , Existing CS Add a link to the software , Encrypt the user name of the current login user and put it in url In the address , stay BS Of login Method by decryption , If it is a legal user, there is no ...

  3. ubuntu Next Java adopt JNI call C

    Here's an example , as follows : public class TestJNI { static { System.loadLibrary("diaoyong"); // When the program is loaded , Automatic loading ...

  4. Java adopt jni Call DLL

    (1)JNI brief introduction JNI yes Java Native Interface Abbreviation , It provides several API Realized Java Communication with other languages ( Mainly C&C++). from Java1.1 Start ,JNI Standards become java ...

  5. Java adopt JNI call C

    Java call C There are many ways , This paper introduces the author's recent learning process , Avoid making similar mistakes in the future . First ,Java It must have called C Dynamic link library for , That is, after compiling by compiler dll/so file . Let's introduce gcc compile dll Methods . General sentiment ...

  6. JAVA Of JNI call

    because JNI call C And call C++ almost , and C++ You can mix and write C Code , So it's mainly about JNI call C++ Part of . General steps : First, write ordinary Java class , This includes local method calls .  And then compile this Java class , transfer ...

  7. Cocos2d-x java adopt jni call c++ Methods

    The previous blog said ,cocos2d-x c++ The interface layer runs on a GLThread In the thread , Strictly speaking, it runs on Cocos2dxGLSurfaceView( Inherited from GLSurfaceView) Inside .opengl My name is ...

  8. About Java adopt JNI call C Dynamic link library (DLL)

    JNI Introduce use JNI Realization Java and C Language data transfer JNI Principle analysis and detailed step screenshot description jni Of JNIEnv Pointers and jobject The pointer JNI Implement callback | JNI call JAVA function | The format of parameters and return values Jni ...

  9. Java adopt JNI call C/C++

    From:http://www.cnblogs.com/mandroid/archive/2011/06/15/2081093.html JNI yes JAVA An important function of the standard platform , It makes up for JAVA Of and ...

Random recommendation

  1. 4. K Line Basics

    1. K Line Basics K Line is also called Yin Yang line . Candlestick Charts . It was first invented by Japanese rice market merchants , Later, it was applied to the analysis of financial market price . K The construction of line graph mainly includes four price factors : Opening price . Closing price . Highest price . The lowest price 2. K Line legend The closing price is higher than the opening price ...

  2. archlinux in c Linguistic rpc Programming

    Reference resources :http://www.cs.rutgers.edu/~pxk/rutgers/notes/rpc/ start-up rpc The server will appear unable to register This mistake , It's said that it's going to start port ...

  3. 6、Android And LayoutInflater.inflate()

    LayoutInflater.inflate() The purpose of this is to make a xml The defined layout file is instantiated as view The control object : And findViewById difference : LayoutInflater.inflate It's loading a ...

  4. linux Set file access control command for ordinary users setfacl

    Command name setfacl  - Set file access control list   Common usage :setfacl [-bkRd] [{-m|-x} acl Parameters ] Destination filename Common parameters of command -m Set up subsequent acl Parameters are used for files ( Commonly used ). ...

  5. Eclipse Set up 、 tuning 、 Use

    eclipse tuning   It's usually wrong eclipse When making the relevant settings , Use eclipse It's always slow to start , It's easy to use , As long as it's right eclipse Some configuration of the relevant parameters of , There will be a big improvement .  Speed up the start-up  1. ...

  6. The form is transparent , But the controls on the form are opaque ( Simple to use )good

    1. stay Delphi in , Set the form's AlphaBlend := true;AlphaBlendValue := 0-255; AlphaBlendValue The smaller the window, the more transparent it is . This method will make the form and form ...

  7. selenium Error prompt after running Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output

    Error message : org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port ...

  8. About vue in tamplate and DOM A brief talk on

    Preface : Before development, use vue when , We can often see template label . Here's a rough description of my attitude towards vue in template Understand and use . 1. First understand vue vue.js It's a light . High performance . The shell component is painted MVVM library . ...

  9. Software projects for the first time sprint Comment on

    Software projects for the first time sprint Comment on Group name : Teletubbies Quartet                                   full name : Gao Changzhi 1. Group name :9-652 Group project : The bomb man game For the bomb man game , First ...

  10. db2 tuning

    The system has been online for about two months , please IBM The engineer did a tuning of the database , The main harvest feelings are as follows : 1, The application server must be separated from the database server 2, If there are multiple databases , Make sure the hard disks are separated (io busy ) 3, Data and logs for each database ...