Java calls C + + (Java call dll) brother Hong teach You

Source: Internet
Author: User

The first step in development is to study the use of Java JNI, because the DLL library needs to be called in Java because of the project to do some graphics processing. Suddenly found that the use of Android Jni like this (hemp egg, not the same, but the need to use the so dynamic link library in Android).

The first step is to create a Java project, name Jnidemo, add Jnidemo.java the second step to add the native method in the Java class
/**@author*/Publicclass  jnidemo{       Public nativestaticvoid set (int  i);      Public native Static int get ();}
Step three using the Javah command to generate the header file

Execute Javah javah com.bk.jnidemo.JniDemo in cmd, generate com_bk_jnidemo_jnidemo.h header file

The fourth step is to create the DLL project using Vs2010

Create Win32 project, project name Jnidemo

Fifth Step copy Com_bk_jnidemo_jnidemo.h,jni.h,jni_md.h to vs Engineering cpp file directory

Where jni.h is in the JDK's include directory, jni_md.h in the JDK Include\win32 directory

Add the header file directory to the project com_bk_jnidemo_jnidemo.h, #include <jni.h> change to #include "jni.h"

Sixth step implementation of C + + method in Jni.cpp

Seventh Step build 64-bit DLL library

Modify Configuration Manager First

Then build the 64dll Library

The eighth step is to copy the 64-bit DLL library to the Java Jnidemo Project root directory and modify the Jnidemo.java

Note the 64-bit DLLs in the X64 directory, and then modify the Jnidemo.java as follows

1  PackageCom.bk.jnidemo;2 3  Public classJnidemo {4     Static{5System.loadlibrary ("Jnidemo");6     }    7      Public native Static voidSetinti);8      Public native Static intget ();9     Ten      Public Static voidMain (string[] args) { OneSet (100); A System.out.println (Get ()); -          -     } the}
Nineth step, get it done.

Java calls C + + (Java call dll) brother Hong teach You

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.