JNI调用从C程序的Java方法 [英] JNI Call java method from c program

查看:139
本文介绍了JNI调用从C程序的Java方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从C程序调用Java方法。我曾尝试以下code调用通过Java本地接口Java方法,但同时面临编译的问题。我是新来C和有经验的Java。所以,我不是能够把自己在创建JVM发生了什么。

I need to call java method from c program. i have tried below code to call the java method through Java native interface but facing issues while compilation. i m new to C and have experience in java. so, i m not able to think myself what is happening while creating JVM.

下面是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'

请帮助我。

推荐答案

该错误消息是关于联阶段,没有编译 - 已包含头文件,而是要创造你必须指定。一个可执行文件(库文件)也。

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.

您有JVM的图书馆链接(添加一些参考libjvm.a到TCC的命令行)。

You have to link with JVM's library (add some reference to libjvm.a to the tcc's command line).

如果您不具备TURBOC ++中的precompiled jvm.lib文件,还有另一种选择 - 用的jvm.dll文件和出口都来自JVM的方法手动链接。这将使用调用LoadLibrary / GetProcAddress的功能。

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. This uses the LoadLibrary/GetProcAddress functions.

有关短采样(对不起,找不到出口的整个源$ C ​​$ C),看看这个:

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

后来使用JNI_CreateJavaVM_ptr代替JNI_CreateJavaVM。你还必须申报 JNI_CreateJavaVM_func 键入 - 你可能会签名从jni.h

Later use the JNI_CreateJavaVM_ptr instead of JNI_CreateJavaVM. Also you'll have to declare the JNI_CreateJavaVM_func type - you might just copy the signature from "jni.h"

这篇关于JNI调用从C程序的Java方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆