Java mini-Problem, using JNI to invoke the corresponding method in local C + + shared library to achieve Yang Hui triangle drawing

Source: Internet
Author: User

1. Configure Javah in Eclipse, configured as follows

Location is the path you javah.exe on your computer's disk

Location: C:\Program Files\java\jdk1.8.0_112\bin\javah.exe

working directory:${PROJECT_LOC}/SRC

independent variables:-classpath.;. /classes-d "${project_loc}/jni"-jni ${java_type_name}

2. Create a Java file

1  PackageExperience5;2 3  Public classNewninetonine {4      Public native voidshowinfo ();5     Static{6         system.loadlibrary ("sy5-1"); 7     }8      Public Static voidMain (string[] args) {9         //TODO auto-generated Method StubTenNewninetonine obj =Newnewninetonine (); One obj.showinfo (); A     } -  - } the  

Note Importing a dynamic-link library does not require a suffix name

To set the native type to a method implemented in C + + or C

3. Select the class to be generated by Javah

(Need to click on it, otherwise it will error: launching Javah has encountered a problem. The selected resourse does not resolve to a JAVA element

Or launching Javah has encountered a problem. Variable references empty selection: ${project_loc}

As long as the click on the good, personally think so wow ". 《)

Then click Run

This will generate a JNI folder in the project folder:

"Experience5_newninetonine.h" is generated in the JNI package.

4. Write a C + + file , I use the wretched dead lost 2013, choose Win32 Console program, create a blank DLL dynamic link library

Copy the previously generated "experience5_newninetonine.h" file,

jni.h files under the C:\Program files (x86) \java\jdk1.8.0_151\include Directory

, and Jni_md.h files in the C:\Program files (x86) \java\jdk1.8.0_151\include\win32 directory are placed in the same directory as Test.cpp

=======================

"Experience5_newninetonine.h" file: note Javah compiled header file is displayed after opening #include<jni.h> because we want to introduce the jni.h file under the JDK, so use double quotes

/*Do not EDIT this file-it are machine generated*/#include "jni.h"/*Header for Class Experience5_newninetonine*/#ifndef _included_experience5_newninetonine#define_included_experience5_newninetonine#ifdef __cplusplusextern "C" {#endif/** Class:experience5_newninetonine * method:showinfo * Signature: ()*/ jniexport  void  jnicall java_experience5_newninetonine_showinfo (jnienv * , jobject); #ifdef __cplusplus}#endif#endif
Create a test.cpp in the source file
jniexport void jnicall java_experience5_newninetonine_showinfo (jnienv *, Jobject);
Is the method declared in Javah generated header file, we need to implement this method in Test.cpp
This code is not optimized, only to the extent that it can be used.

Test.cpp:
1#include <iostream>2#include"experience5_newninetonine.h"3 using namespacestd;4JniexportvoidJnicall Java_experience5_newninetonine_showinfo (jnienv * env, jobject obj)5 {6     inta[ One][ One];7      for(inti =1; I <=Ten; i++) {8a[i][1] =1;9A[i][i] =1;Ten          for(intj =2; j<i; J + +) { OneA[I][J] = a[i-1][j-1] + a[i-1][j]; A         } -  -         if(i>1) { the              for(intz =0; Z < (Ten-i); z++) { -printf"  "); -  -             } +              for(intj =1; J <= I; J + +) { -cout <<"   "<<A[i][j]; +             } A         } at         Elsecout <<"    "<< a[1][1] <<Endl; -  -cout <<Endl; -  -     } -}

What you want to output in Java is in the test.cpp.

Jniexport void Jnicall java_experience5_newninetonine_showinfo (jnienv *env, Jobject obj) method in the body of the implementation
Then the direct compile runs, you will be prompted not to run the. dll file, and the successful build Sy5-1.dll

==================
And then not yet:
Let the Java program successfully call Sy5-1.dll There are two ways, naturally there is a more annoying
1. Copy Sy5-1.dll to the C:\Windows\System32 directory of the system, the default environment variable has this

Configured so you don't have to replicate it once,


==================
One final step:
Restart Eclipse, compile and run. The results are as follows:


Problems that occur:
1. Thrown the Java.lang.UnsatisfiedLinkError exception: No sy1 in Java.library.path, the program did not find the. dll file, lil bit location is no problem
2.can ' t load IA 32-bit. dll on a AMD 64-bit platform error, refer to the online answer, finally I used a 32-bit JDK and then completed;
And then there's an answer in StackOverflow: Https://stackoverflow.com/questions/8113080/cant-load-ia-32-bit-dll-on-a-amd-64-bit-platform.
The inside is compiled with CMD to run, a little bit fascinated, so simply next 32-bit JDK solve everything
3. Slipped away.



Java small topic, using JNI to invoke the corresponding method in the local C + + shared library to achieve the Yang Hui Triangle drawing

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.