Note that there are some explanatory texts on larger screens.

plurals
  1. PONo JNI_OnLoad and no return value from native function?
    primarykey
    data
    text
    <p>i need to make linux command from native method by c++ code.</p> <p>this is my class <b>Test.java</b></p> <pre><code>public class Test { static { System.loadLibrary("NDK1"); } public native String exec(String cmd); } </code></pre> <p>then i create header .h by this command :</p> <pre><code>$ javah -classpath ../bin/classes com.example.new1.Test </code></pre> <p>after this the header .h is created in this path com.example.new1 then i create <b>jni</b> file and copy .h file and create .cpp file and include .h in it.</p> <pre><code>#include &lt;string&gt; #include &lt;iostream&gt; #include &lt;stdio.h&gt; #include &lt;jni.h&gt; #include "com_example_new1_Test.h" std::string exec(const char* cmd) { FILE* pipe = popen(cmd, "r"); if (!pipe) return "ERROR"; char buffer[128]; std::string result = ""; while(!feof(pipe)) { if(fgets(buffer, 128, pipe) != NULL) result += buffer; } pclose(pipe); return result; </code></pre> <p>}</p> <pre><code> JNIEXPORT jstring JNICALL Java_com_example_new1_Test_exec(JNIEnv* env, jobject javaThis , jstring cmd) { const char * res; //res = env-&gt;GetStringUTFChars(cmd ) ; // const char* utf_string; jboolean isCopy; res = env-&gt;GetStringUTFChars(cmd, &amp;isCopy); /* ... use string ... */ if (isCopy == JNI_TRUE) { (env)-&gt;ReleaseStringUTFChars(cmd, res); } std::string result = exec(res); return (env)-&gt;NewStringUTF((const char* )result.c_str()); } </code></pre> <p>then call <b>NDK </b></p> <pre><code>$ /cygdrive/c/android-ndk-r4/ndk-build make: Warning: File `/cygdrive/c/android-ndk-r4/build/core/import-locals.mk' has modification time 140582392 s in the future Cygwin : Generating dependency file converter script Compile++ thumb : NDK1 &lt;= NDK1.cpp Prebuilt : libgnustl_static.a &lt;= &lt;NDK&gt;/sources/cxx-stl/gnu- libstdc++/4.6/libs/armeabi/ SharedLibrary : libNDK1.so Install : libNDK1.so =&gt; libs/armeabi/libNDK1.so Compile++ thumb : NDK1 &lt;= NDK1.cpp Prebuilt : libgnustl_static.a &lt;= &lt;NDK&gt;/sources/cxx-stl/gnu- libstdc++/4.6/libs/armeabi-v7a/ SharedLibrary : libNDK1.so Install : libNDK1.so =&gt; libs/armeabi-v7a/libNDK1.so make: warning: Clock skew detected. Your build may be incomplete. </code></pre> <p>when i run my app there is no error or exception occurred but there is no value return from native method!! this is the log for run </p> <pre><code>03-03 22:59:55.343: E/SpannableStringBuilder(9257): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 03-03 22:59:55.343: E/SpannableStringBuilder(9257): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 03-03 23:00:08.527: E/Trace(9852): error opening trace file: No such file or directory (2) 03-03 23:00:08.717: D/libEGL(9852): loaded /system/lib/egl/libEGL_mali.so 03-03 23:00:08.727: D/libEGL(9852): loaded /system/lib/egl/libGLESv1_CM_mali.so 03-03 23:00:08.727: D/libEGL(9852): loaded /system/lib/egl/libGLESv2_mali.so 03-03 23:00:08.747: D/OpenGLRenderer(9852): Enabling debug mode 0 03-03 23:00:21.960: D/dalvikvm(9852): Trying to load lib /data/data/com.example.new1/lib/libNDK1.so 0x41a1d8f8 03-03 23:00:21.980: D/dalvikvm(9852): Added shared lib /data/data/com.example.new1/lib/libNDK1.so 0x41a1d8f8 03-03 23:00:21.980: D/dalvikvm(9852): No JNI_OnLoad found in /data/data/com.example.new1/lib/libNDK1.so 0x41a1d8f8, skipping init </code></pre> <blockquote> <blockquote> <p>what is the problem ? by the way, i pass "iwconfig" as linux command, is it depend on what is the command ? also do i need any permission at AndroidManifest ?</p> </blockquote> </blockquote>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload