Reputation: 21647
I have a class written in c++. I want to write a method to return a string so I can read it from my java class. I've tried to do something like:
readstring.cpp:
#include "common.h"
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/************
* ReadString *
************/
jstring Java_com_googlecode_leptonica_android_ReadFile__getString(JNIEnv *env, jclass clazz)
{
jstring result = env->NewStringUTF("My String");
return result;
}
#ifdef __cplusplus
}
#endif /* __cplusplus */
Readstring.java:
package com.testjni;
public class ReadString {
static {
System.loadLibrary("lept");
}
public String getText(){
return getString();
}
private static native String getString();
}
Reading string from an activity:
public class TestJNI extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ReadString readString = new ReadString();
Toast.makeText(this, readString.getText(), Toast.LENGTH_SHORT).show();
}
}
and stack trace is:
10-16 12:35:09.974: ERROR/AndroidRuntime(12303): FATAL EXCEPTION: main java.lang.UnsatisfiedLinkError: getString at com.testjni.ReadString.getString(Native Method) at com.testjni.ReadString.getText(ReadString.java:10) at com.testjni.TestJNI.onCreate(TestJNI.java:13) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837) at android.app.ActivityThread.access$1500(ActivityThread.java:132) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:143) at android.app.ActivityThread.main(ActivityThread.java:4196) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) at dalvik.system.NativeStart.main(Native Method)
What am I doing wrong?
Upvotes: 1
Views: 2593
Reputation: 1866
"Java_com_googlecode_leptonica_android_ReadFile__getString"
here your package is " com.googlecode.leptonica.android" and class is "ReadFile" and the function is "getString".
Actually you didnt mention this function in your code .Please check that.
use like this
"Java_com_testjni_ReadString__getString()" in your c code instead of
"Java_com_googlecode_leptonica_android_ReadFile__getString"
Upvotes: 1
Reputation: 20563
This tutorial might help you
http://marakana.com/bookshelf/java_fundamentals_tutorial/_java_native_interface_jni.html
Upvotes: 1
Reputation: 310911
You haven't included the generated .h file in the .c file, and you either didn't generate a .c file at all or you have mangled the function declaration from what was generated.
Upvotes: 0