使用C语言调用Java函数

3
我正在尝试从C代码中调用Java函数。我使用了JNI,就像在http://www.ishaanguliani.com/content/calling-java-functions-c-linux-ubuntu-jni的示例中所讨论的那样。
我使用了相同的代码并遵循了相同的步骤,但出现了无法找到类打印的错误。
我进行了调试,但没有发现我的错误。
在这里分享我的代码。
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代码

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"); 
  }
}

接下来运行以下命令:

unions@universe:~/uni_tmp/jni/vvn$ javac MyJava.java 
unions@universe:~/uni_tmp/jni/vvn$ javah -jni MyJava

当我编译并运行时,我得到了以下输出。
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

我做错了什么?

我也把 options.optionString 改成了这样

options.optionString = "-Djava.class.path=/home/vpraveen/uni_tmp/jni/vvn";

尽管输出结果没有变化。 有什么建议吗?

在哪里尝试?在代码中还是终端中? - optimus prime
尝试将其作为选项字符串。我从未听说过使用“-Djava.class.path”来设置类路径。 - user253751
输出结果为“无法识别的选项:-cp=./无法启动JVM”。 - optimus prime
得到相同的输出:“无法识别选项:-cp ./\n\n无法启动JVM。” - optimus prime
1
我认为 -Djava.class.path 是正确的。 - optimus prime
显示剩余3条评论
1个回答

1
我通过将我的类打包成自己的包来解决这个问题。 当我们没有定义任何包时,它会被视为默认包。 因此,我创建了自己的包,类似于这样。
package com.aqu.vvn

我知道这只是一个权宜之计,但这对我有用。当我找到确切的方法时,我会告诉你。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接