从C语言中调用Java函数 [英] Calling Java functions from C language
问题描述
我搭售从C code调用Java函数。
我用JNI作为的http://www.ishaanguliani.com/content/calling-java-functions-c-linux-ubuntu-jni
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
我用同样的code和遵循相同的步骤,但我越来越无法找到类打印。
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.
在这里分享我的code
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
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");
}
}
在跑这些命令
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
我哪里做错了?
我改变了options.optionString喜欢这个太
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?
推荐答案
我通过我的课到我自己的包解决了这个。
当我们没有定义任何封装它正在为默认的包。
所以,我创建了自己的包像这样
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.
这篇关于从C语言中调用Java函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!