30
#include <stdio.h>
#include <jni.h>

 JNIEnv* create_vm() {
    JavaVM* jvm;
    JNIEnv* env;
    JavaVMInitArgs args;
    JavaVMOption options[1];

    /* There is a new JNI_VERSION_1_4, but it doesn't add anything for the purposes of our example. */
    args.version = JNI_VERSION_1_2;
    args.nOptions = 1;
    options[0].optionString = "-Djava.class.path=/home/test/workspace/pankajs/"
            "jikes/JikesRVMia32-linuxproduction/target/tests/stress/prototype/basic/classes";
    args.options = options;
    args.ignoreUnrecognized = JNI_FALSE;

    JNI_CreateJavaVM(&jvm, (void **)&env, &args);
    return env;
}

void invoke_class(JNIEnv* env) {
    jclass helloWorldClass;
    jmethodID mainMethod;
    jobjectArray applicationArgs;
    jstring applicationArg0;

    helloWorldClass = (*env)->FindClass(env, "/test/org/jikesrvm/basic/core/bytecode/TestSwitch");

    mainMethod = (*env)->GetStaticMethodID(env, helloWorldClass, "main", "([Ljava/lang/String;)V");

    applicationArgs = (*env)->NewObjectArray(env, 1, (*env)->FindClass(env, "java/lang/String"), NULL);
    applicationArg0 = (*env)->NewStringUTF(env, "From-C-program");
    (*env)->SetObjectArrayElement(env, applicationArgs, 0, applicationArg0);

    (*env)->CallStaticVoidMethod(env, helloWorldClass, mainMethod, applicationArgs);
}


int main(int argc, char **argv) {
    JNIEnv* env = create_vm();
    invoke_class( env );
}

I am trying to learn how JVM is actually invoked from bootstrap C files.

I found this example on internet and I am facing some problems while running it.

I am properly specify the build commands as far as I know as,

  g++ -g -I /usr/lib/jvm/java-6-sun-1.6.0.26/include -I /usr/lib/jvm/java-6-sun-1.6.0.26/include/linux CallJVM.c

My intention is to actually run it using jikesrvm but to experiment this I chose to work with JVM. The error I am getting is :

CallJVM.c: In function ‘JNIEnv* create_vm()’:
CallJVM.c:14:4: warning: deprecated conversion from string constant to ‘char*’
CallJVM.c: In function ‘void invoke_class(JNIEnv*)’:
CallJVM.c:28:26: error: base operand of ‘->’ has non-pointer type ‘JNIEnv’
CallJVM.c:30:21: error: base operand of ‘->’ has non-pointer type ‘JNIEnv’
CallJVM.c:32:26: error: base operand of ‘->’ has non-pointer type ‘JNIEnv’
CallJVM.c:32:57: error: base operand of ‘->’ has non-pointer type ‘JNIEnv’
CallJVM.c:33:26: error: base operand of ‘->’ has non-pointer type ‘JNIEnv’
CallJVM.c:34:8: error: base operand of ‘->’ has non-pointer type ‘JNIEnv’
CallJVM.c:36:8: error: base operand of ‘->’ has non-pointer type ‘JNIEnv’

I noticed the different ways of implementing in C and C++ but I think I am writing it correctly.

Edit: On compiling with gcc ,I am getting

undefined reference to `JNI_CreateJavaVM'

and it is what was being prompted in eclipse but I thought my configurations are not apt.When I use ctrl+click it takes me to referenced jni.h as well but still why it is missing reference ? am specifically mentioning my folders on include path.

Pankaj Sejwal
  • 1,605
  • 1
  • 18
  • 27

2 Answers2

84

I noticed the different ways of implementing in C and C++ but I think I am writing it correctly.

You are using the C variant, but are compiling with g++, which invokes the C++ compiler (even if your source file has a .c extension).

Either change the C variants like

(*env)->FindClass(env, ...)

to the C++ variants, like

env->FindClass(...)

or switch your compiler to gcc to compile the source as C code.

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • 1
    thanks...I always thought that C++ compiler can compile C code as well. – Pankaj Sejwal Apr 02 '13 at 13:21
  • 2
    [Besides a couple of cases](http://stackoverflow.com/questions/1201593/c-subset-of-c-where-not-examples), it can - but the issue here is that the `jni` header files assume `C++` code when compiled by a C++ compiler, and therefore provide the `C++` style API, which is not compatible with the `C` style API. Check out `jni.h` - you find a lot of `#ifdef __cplusplus` blocks there – Andreas Fester Apr 02 '13 at 13:35
  • @Andreas How to change it? "switch your compiler to gcc to compile the source as C code". Please tell me. I used Eclipse, use Android project. – Huy Tower Mar 24 '14 at 07:24
  • Assumed that you have created a `C++` project and assumed that you are using Eclipse's internal builder (means not a Makefile based project) it should be sufficient to rename your `.cpp` file to `.c` – Andreas Fester Mar 24 '14 at 08:34
0

I am putting the work around found on other SO posts.. I used straight forwardly this - " gcc -g -I /usr/lib/jvm/java-6-sun-1.6.0.26/include -I /usr/lib/jvm/java-6-sun-1.6.0.26/include/linux -L /usr/lib/jvm/java-6-sun-1.6.0.26/jre/lib/i386/server -ljvm CallJVM.c and a.out was created. Then I need to link it with libjvm.so present in server folder as mentioned in post.

A very beautiful explanation has been put up here

Community
  • 1
  • 1
Pankaj Sejwal
  • 1,605
  • 1
  • 18
  • 27