使用C ++ / CLI从Visual C / C ++调用Java方法 [英] Calling Java Methods from Visual C/C++ using C++/CLI

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

问题描述

我尝试编译VS C ++ / CLI(托管)项目时收到错误LNK1104:无法打开文件{path} \jvm.lib。这很简单,我的目标是在预先存在的java库中调用一些Java方法 - 这里是我使用的代码:

I'm getting a "error LNK1104: cannot open file {path}\jvm.lib" when trying tocompile a VS C++/CLI (managed) project. It's very simple and my goal is to call some Java methods in pre-existing java libs - here is the code I'm using:

// This is the main DLL file.

#include "stdafx.h"
#include <jni_md.h>
#include <jni.h>
#include "JBridge.h"

#pragma once

using namespace System;

namespace JBridge 
{

public ref class JniBridge
{
    // TODO: Add your methods for this class here.


public:
    void HelloWorldTest()
    {
        System::Console::WriteLine("Hello Worldl from managed C++!");
    }

    JNIEnv* create_vm(JavaVM ** jvm) 
    {
        JNIEnv *env;
        JavaVMInitArgs vm_args;

        JavaVMOption options; 
        //Path to the java source code     
        options.optionString = "-Djava.class.path=D:\\Java Src\\TestStruct"; 
        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;
    }
    };
}

我已验证该文件确实存在于路径位置,将它添加到include dir和链接器属性页的项目属性中。

I've verified the file does exist in the path location and I've added it to the project properties for the include dir and the linker property pages.

更新
将jvm.lib设置为链接有点更加fiddling。

Update Got the jvm.lib to be linked with a bit more fiddling.

编译在编译过程中导致以下错误:

Compilation causes following errors during build:

错误1错误LNK2028:未解析的标记(0A00000A)extern Clong __stdcall JNI_CreateJavaVM(struct JavaVM_ * *,void * *,void *)(?JNI_CreateJavaVM @@ $$ J212YGJPAPAUJavaVM _ @@ PAPAXPAX @ Z)引用的函数struct JNIEnv_ * __cdecl create_vm(struct JavaVM_ * *) ?create_vm @@ $$ FYAPAUJNIEnv _ @@ PAPAUJavaVM _ @@@ Z)c:\ Temp \CLRTest\JBridge\JBridge\JBridge.obj JBridge
错误2错误LNK2019:未解析的外部符号extern Clong __stdcall JNI_CreateJavaVM(struct JavaVM_ * *,void * *,void *)(?JNI_CreateJavaVM @@ $$ J212YGJPAPAUJavaVM _ @@ PAPAXPAX @ Z)引用的函数struct JNIEnv_ * __cdecl create_vm(struct JavaVM_ * *) ?create_vm @@ $$ FYAPAUJNIEnv _ @@ PAPAUJavaVM _ @@@ Z)c:\ Temp \CLRTest\JBridge\JBridge\JBridge.obj JBridge
错误3错误LNK1120:2未解决的外部c: \\temp\CLRTest\JBridge\Debug\JBridge.dll JBridge

Error 1 error LNK2028: unresolved token (0A00000A) "extern "C" long __stdcall JNI_CreateJavaVM(struct JavaVM_ * *,void * *,void *)" (?JNI_CreateJavaVM@@$$J212YGJPAPAUJavaVM_@@PAPAXPAX@Z) referenced in function "struct JNIEnv_ * __cdecl create_vm(struct JavaVM_ * *)" (?create_vm@@$$FYAPAUJNIEnv_@@PAPAUJavaVM_@@@Z) c:\Temp\CLRTest\JBridge\JBridge\JBridge.obj JBridge Error 2 error LNK2019: unresolved external symbol "extern "C" long __stdcall JNI_CreateJavaVM(struct JavaVM_ * *,void * *,void *)" (?JNI_CreateJavaVM@@$$J212YGJPAPAUJavaVM_@@PAPAXPAX@Z) referenced in function "struct JNIEnv_ * __cdecl create_vm(struct JavaVM_ * *)" (?create_vm@@$$FYAPAUJNIEnv_@@PAPAUJavaVM_@@@Z) c:\Temp\CLRTest\JBridge\JBridge\JBridge.obj JBridge Error 3 error LNK1120: 2 unresolved externals c:\temp\CLRTest\JBridge\Debug\JBridge.dll JBridge

推荐答案

解决方法是动态加载JVM通过使用LoadLibrary(path / to / jvm);然后调用本机函数。

Work around was to dynamically load the JVM by using LoadLibrary("path/to/jvm"); and then invoking the native functions.

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

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