3

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?

8
  • Try -cp /home/vpraveen/uni_tmp/jni/vvn? Commented Feb 23, 2016 at 11:06
  • Where to try that? In code or in terminal? Commented Feb 23, 2016 at 11:15
  • Try that as the options string. I've never heard of using -Djava.class.path to set the classpath. Commented Feb 23, 2016 at 11:18
  • The output was "Unrecognized option: -cp=./ Unable to Launch JVM" Commented Feb 23, 2016 at 11:24
  • 1
    I think that -Djava.class.path is the right one Commented Feb 23, 2016 at 11:35

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.