问题描述
我正在编写一个简单的程序来调用我的C程序中的Java函数.
以下是我的代码:
#include <jni.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/mman.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <pthread.h> #include <time.h> #include <stdlib.h> JNIEnv* create_vm() { JavaVM* jvm; JNIEnv *env; JavaVMInitArgs vm_args; JavaVMOption options[1]; options[0].optionString - "-Djava.class.path=/home/chanders/workspace/Samples/src/ThreadPriorityTest"; vm_args.version = JNI_VERSION_1_6; vm_args.nOptions = 1; vm_args.options = &options; vm_args.ignoreUnrecognized = JNI_FALSE; JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args); return env; } void invoke_class(JNIEnv* env) { jclass helloWorldClass; jmethodID mainMethod; jobjectArray applicationArgs; jstring applicationArg0; helloWorldClass = (*env)->FindClass(env, "InvocationHelloWorld"); 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() { JNIEnv* env = create_vm(); invoke_class(env); }
我正在使用: GCC -O Invoke -i $ java_home/include/-i $ java_home/include/linux -l $ java_home/jre/lib/lib/amd64/server/server/threadprioritytest.c
我会收到以下错误:
/tmp/ccllsK5O.o: In function `create_vm': ThreadPriorityTest.c:(.text+0x35): undefined reference to `JNI_CreateJavaVM' collect2: ld returned 1 exit status
我不确定是什么原因导致了这个问题
更新1
在命令行中包括-ljvm,然后获得undefined reference to FUNCTION_NAME 我在RHEL 6.2
上运行它推荐答案
您将路径带到Java库(-L选项),但库本身没有.您还需要在链接行中包括-ljvm.