public class NativeCode { // loadLibrary // NativeCode static { System.loadLibrary( "nativecode" ); } public NativeCode() { // srand srand(); } // public native int getInt(); // int public native void showInt(int i); // int public native void showIntArray(int[] array); // public native int getRandomInt(); // public native void addOneToArray(int[] array); private native void srand(); }
javac NativeCode.java javah -jni -o NativeCode.h NativeCode
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class by_framework_nativeapp_NativeCode */ #ifndef _Included_by_framework_nativeapp_NativeCode #define _Included_by_framework_nativeapp_NativeCode #ifdef __cplusplus extern "C" { #endif /* * Class: by_framework_nativeapp_NativeCode * Method: getInt * Signature: ()I */ JNIEXPORT jint JNICALL Java_by_framework_nativeapp_NativeCode_getInt (JNIEnv *, jobject); /* * Class: by_framework_nativeapp_NativeCode * Method: showInt * Signature: (I)V */ JNIEXPORT void JNICALL Java_by_framework_nativeapp_NativeCode_showInt (JNIEnv *, jobject, jint); /* * Class: by_framework_nativeapp_NativeCode * Method: showIntArray * Signature: ([I)V */ JNIEXPORT void JNICALL Java_by_framework_nativeapp_NativeCode_showIntArray (JNIEnv *, jobject, jintArray); /* * Class: by_framework_nativeapp_NativeCode * Method: getRandomInt * Signature: ()I */ JNIEXPORT jint JNICALL Java_by_framework_nativeapp_NativeCode_getRandomInt (JNIEnv *, jobject); /* * Class: by_framework_nativeapp_NativeCode * Method: addOneToArray * Signature: ([I)V */ JNIEXPORT void JNICALL Java_by_framework_nativeapp_NativeCode_addOneToArray (JNIEnv *, jobject, jintArray); /* * Class: by_framework_nativeapp_NativeCode * Method: srand * Signature: ()V */ JNIEXPORT void JNICALL Java_by_framework_nativeapp_NativeCode_srand (JNIEnv *, jobject); #ifdef __cplusplus } #endif #endif
#include <iostream> #include <ctime> #include <cstdlib> #include <iomanip> #include "NativeCode.h" JNIEXPORT jint JNICALL Java_by_framework_nativeapp_NativeCode_getInt (JNIEnv *enc, jobject obj) { int input = 1; std::cout<<"Input number: "; std::cin>>input; if(input<0) input = 0; return input; } JNIEXPORT void JNICALL Java_by_framework_nativeapp_NativeCode_showInt (JNIEnv *env, jobject obj, jint i) { std::cout<<"Output number: "<<i<<std::endl; } JNIEXPORT void JNICALL Java_by_framework_nativeapp_NativeCode_showIntArray (JNIEnv *env, jobject obj, jintArray jarray) { int len = env->GetArrayLength(jarray); std::cout<<"Array length: "<<len<<std::endl; jint* arr = env->GetIntArrayElements(jarray, 0); for(int i = 0; i < len; i++) { std::cout<<std::setw(5)<<i<<": "<<std::setw(4)<<arr[i]<<std::endl; } env->ReleaseIntArrayElements(jarray, arr, 0); } JNIEXPORT jint JNICALL Java_by_framework_nativeapp_NativeCode_getRandomInt (JNIEnv *env, jobject obj) { int i = rand()%100; return i; } JNIEXPORT void JNICALL Java_by_framework_nativeapp_NativeCode_addOneToArray (JNIEnv *env, jobject obj, jintArray jarray) { int len = env->GetArrayLength(jarray); jint* arr = env->GetIntArrayElements(jarray, 0); for(int i = 0; i < len; i++) { ++(*(arr+i)); } // .. GetIntArrayElements , // Java env->ReleaseIntArrayElements(jarray, arr, 0); } JNIEXPORT void JNICALL Java_by_framework_nativeapp_NativeCode_srand (JNIEnv *env, jobject obj) { srand(time(NULL)); }
g++ -o libnativecode.o -I"/usr/lib/jvm/java-1.7.0-openjdk-amd64/include" -I"/usr/lib/jvm/java-1.7.0-openjdk-amd64/include/linux" -fpic -c NativeCode.cpp g++ -o libnativecode.so -shared libnativecode.o
public class AppClass { public static void main(String[] args) { // NativeCode // NativeCode nc = new NativeCode(); int i = nc.getInt(); nc.showInt(++i); int[] array = new int[i]; // for(int j = 0; j < i; j++) { array[j] = nc.getRandomInt(); } nc.showIntArray(array); nc.addOneToArray(array); nc.showIntArray(array); } }
javac AppClass.java
java -Djava.library.path="." AppClass
all : NativeCode.so NativeCode.so : NativeCode.obj g++ -o bin/libnativecode.so -shared bin/libnativecode.o NativeCode.obj: cpp_src/NativeCode.cpp java_headers g++ -o bin/libnativecode.o -I"/usr/lib/jvm/java-1.7.0-openjdk-amd64/include" -I"/usr/lib/jvm/java-1.7.0-openjdk-amd64/include/linux" -fpic -c cpp_src/NativeCode.cpp java_headers: java_class_files javah -jni -o cpp_src/NativeCode.h -classpath bin by.framework.nativeapp.NativeCode java_class_files: src/by/framework/nativeapp/NativeCode.java src/by/framework/nativeapp/AppClass.java mkdir -p bin javac -d bin -cp bin src/by/framework/nativeapp/NativeCode.java javac -d bin -cp bin src/by/framework/nativeapp/AppClass.java
Source: https://habr.com/ru/post/160293/
All Articles