[英]JNI Call java method from c program
I need to call java method from c program. 我需要从C程序调用Java方法。 i have tried below code to call the java method through Java native interface but facing issues while compilation. 我试过下面的代码通过Java本机接口调用Java方法,但是在编译时遇到问题。 im new to C and have experience in java. 我是C语言新手,并且有Java经验。 so, im not able to think myself what is happening while creating JVM. 因此,在创建JVM时,我无法自已。
Below is the code. 下面是代码。
CTest.c
#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=D:\\Ashish_Review\\JNI\\src";
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 );
}
C:\Users\Desktop\tcc>tcc C:\TurboC++\Disk\TurboC3\BIN\CTest.c -I "C:\Program Files\Java\jdk1.6.0_16\include" -I "C:\Program Files\Java\jdk1.6.0_16\include\win32" -shared -o CTest.dll
tcc: undefined symbol '_JNI_CreateJavaVM@12'
please help me out. 请帮帮我。
The error message is about the linking stage, not compilation - you have included the header file, but to create the executable you have to specify the .a (library files) also. 错误消息与链接阶段有关,而不是编译阶段-您已包含头文件,但是要创建可执行文件,您还必须指定.a(库文件)。
You have to link with JVM's library (add some reference to libjvm.a to the tcc's command line). 您必须链接到JVM的库(在tcc的命令行中添加对libjvm.a的一些引用)。
If you don't have a precompiled jvm.lib file for TurboC++, there is another option - link with the jvm.dll file and export all the methods from JVM manually. 如果没有用于TurboC ++的预编译的jvm.lib文件,则还有另一个选项-与jvm.dll文件链接,并手动从JVM导出所有方法。 This uses the LoadLibrary/GetProcAddress functions. 这将使用LoadLibrary / GetProcAddress函数。
For a short sample (sorry, couldn't find the entire export source code), look at this: 对于一个简短的示例(很抱歉,找不到完整的导出源代码),请查看以下内容:
/* load library */
HMODULE dll = LoadLibraryA("jvm.dll");
/* declare a function pointer and initialize it with the "pointer" to dll's code */
JNI_CreateJavaVM_func JNI_CreateJavaVM_ptr = GetProcAddress(dll, "JNI_CreateJavaVM");
Later use the JNI_CreateJavaVM_ptr instead of JNI_CreateJavaVM. 以后使用JNI_CreateJavaVM_ptr代替JNI_CreateJavaVM。 Also you'll have to declare the JNI_CreateJavaVM_func
type - you might just copy the signature from "jni.h" 另外,您还必须声明JNI_CreateJavaVM_func
类型-您可能只是从“ jni.h”复制签名
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.