Click here to Skip to main content
15,671,597 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi Dear,
I am creting an android application over JNI.i am created some files in java likes

package com.allwikan.runkey;

import com.allwikan.runkey.MainActivity;
import com.allwikan.runkey.R;

import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
	 private LinearLayout linearLayout;
		private   InputMethodManager inputMethodManager;
		 private Keyboard lkeyboard;
    public void onCreate(Bundle savedInstanceState) {
        lkeyboard=new Keyboard(MainActivity.this);
        Button button=(Button)findViewById(;

     button.setOnClickListener(new OnClickListener() {
	public void onClick(View v) {
		// TODO Auto-generated method stub
		// inputMethodManager.toggleSoftInputFromWindow(linearLayout.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(, menu);
        return true;

package com.allwikan.runkey;
import android.R.bool;
import android.content.Context;
import android.view.inputmethod.InputMethodManager;
public class Keyboard {

     static {

    Context mContext;
     public Keyboard(Context mContext){
           this.mContext = mContext;

      public  void  getSystemService(bool makevisible){
         if(makevisible != null){
            InputMethodManager m = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            m.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
             InputMethodManager m = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            m.toggleSoftInput(0, 0);
      public native void  openKeyBoard();

It display the keyboard in emuletor.but my requirement call by c over jni then we created two file .likes


/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_allwikan_runkey_Keyboard */

#ifndef _Included_com_allwikan_runkey_Keyboard
#define _Included_com_allwikan_runkey_Keyboard
#ifdef __cplusplus
extern "C" {
 * Class:     com_allwikan_runkey_Keyboard
 * Method:    openKeyBoard
 * Signature: ()V
JNIEXPORT void JNICALL Java_com_allwikan_runkey_Keyboard_openKeyBoard
  (JNIEnv *, jobject);

#ifdef __cplusplus


#include <com_allwikan_runkey_keyboard.h>
#include "stdio.h"

#include "jni.h"

#define  LOG_TAG    "com_allwikan_runkeyboard_Keyboard"
#define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
int main()

	JNIEnv *env;
	     JavaVM *jvm;
	     jint res;
	     jclass cls;
	     jmethodID mid;
	     jstring jstr;
	     jclass stringClass;
	     jobjectArray args;

	 #ifdef JNI_VERSION_1_2
	     JavaVMInitArgs vm_args;
	     JavaVMOption options[1];
	   //  options[0].optionString = ("-Djava.class.path=") USER_CLASSPATH;
	     vm_args.version = 0x00010002;
	     vm_args.options = options;
	     vm_args.nOptions = 1;
	     vm_args.ignoreUnrecognized = JNI_TRUE;
	     /* Create the Java VM */
	     res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
	     JDK1_1InitArgs vm_args;
	     char classpath[1024];
	     vm_args.version = 0x00010001;
	     /* Append USER_CLASSPATH to the default system class path */
	     sprintf(classpath, "%s%c%s",
	             vm_args.classpath, PATH_SEPARATOR, USER_CLASSPATH);
	     vm_args.classpath = classpath;
	     /* Create the Java VM */
	     res = JNI_CreateJavaVM(&jvm, &env, &vm_args);
	 #endif /* JNI_VERSION_1_2 */

	     if (res < 0) {
	         fprintf(stderr, "Can't create Java VM\n");
	     cls = (*env)->FindClass(env, "Prog");
	     if (cls == NULL) {
	         goto destroy;

	     mid = (*env)->GetStaticMethodID(env, cls, "main",
	     if (mid == NULL) {
	         goto destroy;
	     jstr = (*env)->NewStringUTF(env, " from C!");
	     if (jstr == NULL) {
	         goto destroy;
	     stringClass = (*env)->FindClass(env, "java/lang/String");
	     args = (*env)->NewObjectArray(env, 1, stringClass, jstr);
	     if (args == NULL) {
	         goto destroy;
	     (*env)->CallStaticVoidMethod(env, cls, mid, args);

	     if ((*env)->ExceptionOccurred(env)) {


When compile this code by ndk-build commond its give error

/home/allwikan/Documents/eclipse_workspace/RunKey/jni/com_allwikan_runkey_Keyboard.c:33: undefined reference to `JNI_CreateJavaVM'
collect2: ld returned 1 exit status

Please help me.
(1)How to resolve this error
(2)how to keyboard display in emulator
(3)which function used in c for display keyboard.

Thanks ...

I have also two file like

LOCAL_PATH :=$(call my-dir)
include $(call all-subdir-makefiles)

include $(CLEAR_VARS)

LOCAL_LDLIBS := -llog -ldl
LOCAL_MODULE := com_allwikan_runkey_Keyboard
LOCAL_SRC_FILES := com_allwikan_runkey_Keyboard.c
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog


APP_ABI :=all

then where am i wrong.

please help me....
Updated 15-Sep-12 3:28am
Richard MacCutchan 15-Sep-12 11:22am    
Where is JNI_CreateJavaVM() defined? Probably in some JNI system library which you have not added to your build parameters, as I explained earlier.

1 solution

You are missing a library or LIB directive in your project, so the linker cannot resolve the reference. However, I am not sure that you can directly load a C executable into an Android system.
Share this answer
Tn1111 15-Sep-12 12:37pm    
Thanks for giving reply
but i have also file where BUILD_SHARED_LIBRARY so the code is compiling but gives on error
undefined reference to `JNI_CreateJavaVM' so how to resoolve it.

Richard MacCutchan 15-Sep-12 12:51pm    
How many more times do I need to explain that you are missing a library in your build, which contains the definition for the missing reference? If you do not know how to compile and link a program then take a look at the documentation, starting with the man pages for make and ld.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900