Reputation: 774
I am tying to call java functions from c code. I used the JNI as discussed in the example at http://www.ishaanguliani.com/content/calling-java-functions-c-linux-ubuntu-jni
I used the same code and followed the same steps but I am getting unable to find the class print.
I debugged but I didnt find what I did wrong.
Sharing my code here
unions@universe:~/uni_tmp/jni/vvn$ cat MyC.c
#include <stdio.h>
#include <jni.h>
#include "MyJava.h"
#include <string.h>
JNIEnv* create_vm(JavaVM ** jvm) {
JNIEnv *env;
JavaVMInitArgs vm_args;
JavaVMOption options;
options.optionString = "-Djava.class.path=./"; //Path to the java source code
vm_args.version = JNI_VERSION_1_6; //JDK version. This indicates version 1.6
vm_args.nOptions = 1;
vm_args.options = &options;
vm_args.ignoreUnrecognized = 0;
int ret = JNI_CreateJavaVM(jvm, (void**)&env, &vm_args);
if(ret < 0)
printf("\nUnable to Launch JVM\n");
return env;
}
int main(void)
{
JNIEnv *env;
JavaVM *jvm;
jmethodID mainMethod = NULL;
jmethodID smfnMethod = NULL;
jclass clsJava=NULL;
jstring StringArg=NULL;
env = create_vm(&jvm);
if (env == NULL)
{
printf("\n Unable to create environment");
return 1;
}
clsJava = (*env)->FindClass(env,"MyJava");
if (clsJava != NULL)
{
printf("\n Able to find the requested class\n");
} else {
printf("\n Unable to find the requested class\n");
return 0;
}
mainMethod = (*env)->GetStaticMethodID(env, clsJava, "main", " ([Ljava/lang/String;)V");
smfnMethod = (*env)->GetStaticMethodID(env, clsJava,"sampleStaticFunc", "(Ljava/lang/String;)V");
if (mainMethod != NULL)
{
printf("\n Calling the Java Main method");
(*env)->CallStaticVoidMethod(env, clsJava, mainMethod, NULL);
}
StringArg = (*env)->NewStringUTF(env, "Argument from C");
if (smfnMethod != NULL)
{
printf("\n Calling the Static Function method");
(*env)->CallStaticVoidMethod(env, clsJava, smfnMethod, StringArg);
}
printf("\n End C main \n");
return 0;
}
Java code
cat unions@universe:~/uni_tmp/jni/vvn$ cat MyJava.java
public class MyJava
{
public MyJava()
{
System.out.println("\n Inside the constrcutor of Java Function \n ");
}
private void sampleFunc(String str)
{
System.out.println("\n Inside sampleFunc value of string = " + str);
}
public static void sampleStaticFunc(String str)
{
System.out.println("\n Inside static sampleFunc value of string = " + str);
}
public static void main(String[] args)
{
MyJava obj = new MyJava();
obj.sampleFunc("Ishaan is my name");
System.out.println("\n Calling Java from C function \n");
}
}
After that Ran these commands
unions@universe:~/uni_tmp/jni/vvn$ javac MyJava.java
unions@universe:~/uni_tmp/jni/vvn$ javah -jni MyJava
When I compiled and Ran I got this output
export LD_LIBRARY_PATH=/usr/lib/jvm/java-6-openjdk-amd64/jre/lib/amd64/server
unions@universe:~/uni_tmp/jni/vvn$ gcc -I /usr/lib/jvm/java-6-openjdk-amd64/include -I /usr/lib/jvm/java-6-openjdk-amd64/include/linux -L /usr/bin/java -L /usr/lib/jvm/java-6-openjdk-amd64/jre/lib/amd64/server MyC.c -ljvm ; ./a.out
Unable to find the requested class
Where I did wrong?
I changed the options.optionString to like this too
options.optionString = "-Djava.class.path=/home/vpraveen/uni_tmp/jni/vvn";
Even though There is no change in the output. Any suggestions?
Upvotes: 3
Views: 3794
Reputation: 774
I solved this by making my class into my own package. When we did not define any package it is taking as default package. So I created my own package something like this
package com.aqu.vvn
I know its a work around but doing this worked for me. I will let u know the exact way when I figured out.
Upvotes: 1