Tutorial on using JNI under Windows

Source: Internet
Author: User

The use of JNI generally has the following 4 steps:

First, write the native method in Java

Second, the C + + header file generated with the Javah command

Third, write the corresponding C + + program to implement the method declared in the header file, and compile into a library file

Iv. load the library file in Java and use

Tools:

1. Eclipse for Java

2. Visual Studio 2015

Depend on:

JDK and jre:1.8.0_144 versions

Steps:

Create a new Java project in eclipse

Add a Java class, placed under the COM.JNI package

Note: Java code should not be placed under the default package (that is, the package will not be written into the default packages), the default package under the method can not be called anywhere else!!

Write native method, native method is to declare a non-Java implementation method, such as the implementation

1  Package Com.jni; 2 3  Public class JNI {4      Public native String getString (); 5 }

At Eclipse Generation, the corresponding. class file is generated in the project directory, and Eclipse places the. class file under the Bin folder of the project directory

Open cmd and go to Bin directory

Run the Javah command, generate a C + + header file, and note the Java package name

Note: Here is the bin directory running Javah, cannot enter com/jni down to run Javah, otherwise the header file cannot be generated.

The JNI.h header file is generated in the bin directory at this time

The contents of the header file are as follows:

1 /*Do not EDIT this file-it are machine generated*/2#include <jni.h>3 /*Header for Class Com_jni_jni*/4 5 #ifndef _included_com_jni_jni6 #define_included_com_jni_jni7 #ifdef __cplusplus8 extern "C" {9 #endifTen /* One * Class:com_jni_jni A * method:getstring - * Signature: () ljava/lang/string; -  */ the jniexport jstring jnicall java_com_jni_jni_getstring -(JNIENV *, jobject); -  - #ifdef __cplusplus + } - #endif + #endif

Next Open Visual Studio 2015, create an empty Win32 console program, select the DLL in the program type:

To configure the JNI development environment:

In the Include directory, VC + + directory, project settings, add the path to the JNI header file

JNI header files are typically placed in the Include folder under the JDK directory, as well as the Win32 folder under the Include folder

Note Release and debug as well as 32-bit and 64-bit options

Copy the header file you just generated to the VS project directory, add the header file to the project, and add the functions in the CPP implementation header file

The MAIN.CPPD code is as follows:

" com_jni_jni.h "  *env, Jobject obj) {    return env->newstringutf ("string from C + + " );}

Build the project, build the JNI.dll dynamic library under the x64 or Release folder of the project directory, and tell the DLL file to be copied to the Eclipse project directory

Modify the Jni.java code, load the JNI library, and add the running code

1  PackageCom.jni;2 3  Public classJNI {4     Static {5System.loadlibrary ("JNI");6     }7     8      Public nativeString getString ();9     Ten      Public Static voidMain (String args[]) { OneJNI JNI =NewJNI (); A System.out.println (jni.getstring ()); -     } -}

Build and run, you can see the string returned from C + +

Tutorial on using JNI under Windows

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.