1) create a Java file:
public class Hello{
static {
//System.setProperty("java.library.path", ".");
System.loadLibrary("hello");
}
public static void main(String[] args){
new Hello().sayHello();
}
public native void sayHello();
public static String get(int index){
System.out.println("===="+index);
//return null;
return null;
}
}
2)compile JAVA code
javac Hello.java
3)create an CPP header file Hello.h
#include <jni.h>
/* Header for class HelloJni */
#ifndef _Included_HelloJni
#define _Included_HelloJni
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT void JNICALL Java_Hello_sayHello (JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
4)create an CPP file Hello.cpp
#include "Hello.h"
JNIEXPORT void JNICALL Java_Hello_sayHello(JNIEnv *env, jclass cls)
{
printf("HelloWorld");
//invoke the method from JAVA class
cls = env->FindClass("Hello");
//Here the 3rd parameter is the signature of a method which could get by 'javap -s -private Hello'
jmethodID mid_getFirst = env->GetStaticMethodID(cls,"get","(I)Ljava/lang/String;");
//jobject obj = env->AllocObject(cls); //here is trying to allocate a space for new object
jstring jfirst = (jstring)env->CallStaticObjectMethod(cls, mid_getFirst,10);
mid_getFirst = env->GetStaticMethodID(cls,"get","(I)Ljava/lang/String;");
jfirst = (jstring)env->CallStaticObjectMethod(cls, mid_getFirst, 10);
printf("\nsuccess\n");
}
5)compile CPP code
g++ -I $JAVA_HOME/include -I $JAVA_HOME/include/linux Hello.cpp -fPIC -shared -o libhello.so
6)run JAVA file
java -Djava.library.path=. Hello
Have a nice day!
public class Hello{
static {
//System.setProperty("java.library.path", ".");
System.loadLibrary("hello");
}
public static void main(String[] args){
new Hello().sayHello();
}
public native void sayHello();
public static String get(int index){
System.out.println("===="+index);
//return null;
return null;
}
}
2)compile JAVA code
javac Hello.java
3)create an CPP header file Hello.h
#include <jni.h>
/* Header for class HelloJni */
#ifndef _Included_HelloJni
#define _Included_HelloJni
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT void JNICALL Java_Hello_sayHello (JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
4)create an CPP file Hello.cpp
#include "Hello.h"
JNIEXPORT void JNICALL Java_Hello_sayHello(JNIEnv *env, jclass cls)
{
printf("HelloWorld");
//invoke the method from JAVA class
cls = env->FindClass("Hello");
//Here the 3rd parameter is the signature of a method which could get by 'javap -s -private Hello'
jmethodID mid_getFirst = env->GetStaticMethodID(cls,"get","(I)Ljava/lang/String;");
//jobject obj = env->AllocObject(cls); //here is trying to allocate a space for new object
jstring jfirst = (jstring)env->CallStaticObjectMethod(cls, mid_getFirst,10);
mid_getFirst = env->GetStaticMethodID(cls,"get","(I)Ljava/lang/String;");
jfirst = (jstring)env->CallStaticObjectMethod(cls, mid_getFirst, 10);
printf("\nsuccess\n");
}
5)compile CPP code
g++ -I $JAVA_HOME/include -I $JAVA_HOME/include/linux Hello.cpp -fPIC -shared -o libhello.so
6)run JAVA file
java -Djava.library.path=. Hello
//To debug the core file we use the command gdb then tc
Have a nice day!