简体   繁体   English

JNI与Builder C ++

[英]JNI with Builder C++

I need to use the Java Native Interface (JNI) to communicate between my C++ application and my Java app. 我需要使用Java本机接口(JNI)在C ++应用程序和Java应用程序之间进行通信。

I started with generating my JVM.Lib from my JVM.DLL . 我开始与我产生JVM.Lib从我JVM.DLL For that I used impdef and implib . 为此,我使用了impdefimplib I add my setting (Include path and Library path) in my project Builder C++. 我在我的项目Builder C ++中添加了设置(包括路径和库路径)。

However, my program generates 但是,我的程序会生成

Error link JNI_CreateJavaVM. 错误链接JNI_CreateJavaVM。

How do I fix this error? 如何解决此错误? How do I use JNI in my application Builder C++? 如何在我的Application Builder C ++中使用JNI?

I succeed to fix this error.. I used Coff2Omf command to generate my lib 我成功解决了此错误。.我使用Coff2Omf命令生成了我的库

coff2omf jvm.lib jvm2.lib coff2omf jvm.lib jvm2.lib

After that, i added jvm2.lib and include path in my setting.. 之后,我添加了jvm2.lib并在设置中包含路径。

this is a sample C++ code to calling method java : 这是调用方法java的示例C ++代码:

     #include <windows.h>
     #include <stdio.h>
     #include <jni.h>
     #include <string.h>

     #define PATH_SEPARATOR ';' /* define it to be ':' on Solaris */
     #define USER_CLASSPATH "." /* where Prog.class is */

     typedef /*_JNI_IMPORT_OR_EXPORT_*/ jint (JNICALL *JNI_CreateJavaVM_func)(JavaVM **pvm, void **penv, void *args);

     JNI_CreateJavaVM_func JNI_CreateJavaVM_ptr;

    JNIEnv* create_vm(JavaVM ** jvm)
    {
        JNIEnv *env;
        JavaVMInitArgs vm_args;
        JavaVMOption options;
        memset(&vm_args, 0, sizeof(vm_args));
        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;

        HMODULE jvm_dll = LoadLibrary("C:\\Program Files (x86)\\Java\\jdk1.7.0_65\\jre\\bin\\server\\jvm.dll");

        /// You might check the GetLastError() here after the LoadLibrary()
        if(jvm_dll == NULL) 
        { 
            printf("can't load dll\n"); 
            exit(1); 
        }

        JNI_CreateJavaVM_ptr = (JNI_CreateJavaVM_func)GetProcAddress(jvm_dll, "JNI_CreateJavaVM");

        /// You might check the GetLastError() here
        if(JNI_CreateJavaVM_ptr == NULL) 
        { 
            printf("can't load function\n"); 
            exit(1); 
        }

        int ret = JNI_CreateJavaVM_ptr(jvm, (void**)&env, &vm_args);
        if(ret < 0) 
        { 
            printf("\nUnable to Launch JVM\n"); 
        }
        return env;
    }

    int main(int argc, char* argv[])
    {
        JNIEnv *env;
        JavaVM * jvm;
        env = create_vm(&jvm);

        if (env == NULL) { return 1; }
        jclass cls;
        jmethodID mid;
        jint square;
        jboolean not;
        cls = (*env).FindClass("Sample2");

        if(cls !=0)
          {   mid = (*env).GetStaticMethodID(cls, "intMethod", "(I)I");
                if(mid !=0)
                {  square = (*env).CallStaticIntMethod(cls, mid, 5);
                   printf("Result of intMethod: %d\n", square);
                }

                mid = (*env).GetStaticMethodID(cls, "booleanMethod", "(Z)Z");
                if(mid !=0)
                {  not = (*env).CallStaticBooleanMethod( cls, mid, 1);
                   printf("Result of booleanMethod: %d\n", not);
                }
          }

        int n = jvm->DestroyJavaVM();
        return 0;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM