trouble with Java Native Interfaces

29 views
Skip to first unread message

Girish Joshi

unread,
Jun 16, 2014, 9:58:46 AM6/16/14
to jugn...@googlegroups.com
hello
I'm trying to call a java method from c++ code.
this is the first time i'm using JNI so just copied the helloworld code from net.
my problem is when i try to compile the c code it gives me error


undefined reference to `JNI_CreateJavaVM'
collect2: error: ld returned 1 exit status


the code is


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

JNIEnv* create_vm() {
    JavaVM* jvm;
    JNIEnv* env;
    JavaVMInitArgs args;
    JavaVMOption options[1];
    args.version = JNI_VERSION_1_6;
    args.nOptions = 1;
    options[0].optionString = "-Djava.class.path=/home/girish/Desktop/jnitest";
    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, "HelloWorld");

    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'm compiling this code with

gcc -o test  -I /usr/lib/jvm/java-7-openjdk-i386/include/ -I /usr/lib/jvm/java-7-openjdk-i386/include/linux/ -L /usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/client/ -ljvm  -L /usr/bin/java test.c


so my first question is how to fix this? i've made sure that libjvm.so is present in  /usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/client/ .
and my second question is libjvm.so is present in three directories  /usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/client/ , /usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/server/  and
/usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/jamvm/ which one I need to compile this code.
can some buddy help me?

thank you.
girish.

Tushar Joshi

unread,
Jun 17, 2014, 1:08:16 AM6/17/14
to JUG Nagpur
Hello Girish,

I am waiting for someone who has already done this and can answer.  I will also try to do one native use case like this on next weekend.  For that I may need your help for the linux based C compilation things

Lets see if anyone has some time to answer here, this week


--
--
---------------------------------------------------------------------------------------------------------------
Java User Group Nagpur
Home Page: http://www.jugnagpur.com
Blog: http://jugnagpur.blogspot.com
Wiki: http://jugnagpur.wikispaces.com/
Project Page: https://jugnagpur.dev.java.net/
---------------------------------------------------------------------------------------------------------------
You received this message because you are subscribed to JUGNagpur group.
To post to this group, send email to jugn...@googlegroups.com
To unsubscribe from this group, send email to
jugnagpur+...@googlegroups.com
 
For more options, visit this group at
http://groups.google.com/group/jugnagpur?hl=en
---
You received this message because you are subscribed to the Google Groups "jugnagpur" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jugnagpur+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
with regards
    Tushar


Tushar Joshi, Nagpur: http://www.tusharvjoshi.com

* MCSD_NET C#, SCJP, RHCE, ZCE, PMP, CSM

* Senior Architect: http://www.persistentsys.com
* NetBeans Dream Team Member: http://dreamteam.netbeans.org
* Java User Group Nagpur Founder and Leader: http://www.jugnagpur.com
* Nagpur ACM Chapter Membership chair: http://www.acm.org

Girish Joshi

unread,
Jun 18, 2014, 4:53:19 AM6/18/14
to jugn...@googlegroups.com
sure,
i guess there is some problem with library ie. libjvm.so (i'm not getting exactly which library i need to load).

girish.

Girish Joshi

unread,
Jun 21, 2014, 12:26:44 PM6/21/14
to jugn...@googlegroups.com
as i can not call   JNI_CreateJavaVM(&jvm, (void **)&env, &args);  from the above cod ie. it is not letting me create a new virtual machine instance so
i removed that function called a c function from java; to do that jni passes pointer to virtual-machine-environment.
using the same instance of jvm i called the java method from c.
i don't know if this is the way to do it, but right now it's working for me.

girish.
HelloJNI.java
twoway.c
Reply all
Reply to author
Forward
0 new messages