尝试编译 VS C++/CLI(托管)项目时,出现“错误 LNK1104:无法打开文件 {path}\jvm.lib”。这非常简单,我的目标是调用预先存在的 java 库中的一些 Java 方法 - 这是我正在使用的代码:
// 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;
}
};
}
我已验证该文件确实存在于路径位置中,并将其添加到包含目录和链接器属性页的项目属性中。
更新 通过更多的摆弄来链接jvm.lib。
编译在构建过程中导致以下错误:
错误 1 错误 LNK2028:无法解析的标记 (0A00000A) “extern "C"long __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:函数“struct JNIEnv_ * __cdecl create_vm(struct JavaVM_ * *)”(?create_vm@@$) 中引用了无法解析的外部符号“extern "C"long __stdcall JNI_CreateJavaVM(struct JavaVM_ * *,void * *,void *)"(?JNI_CreateJavaVM@@$$J212YGJPAPAUJavaVM_@@PAPAXPAX@Z) $FYAPAUJNIEnv_@@PAPAUJavaVM_@@@Z) c:\Temp\CLRTest\JBridge\JBridge\JBridge.obj JBridge 错误 3 错误 LNK1120:2 个无法解析的外部 c:\temp\CLRTest\JBridge\Debug\JBridge.dll JBridge
最佳答案
解决方法是使用 LoadLibrary("path/to/jvm"); 动态加载 JVM;然后调用原生函数。
关于java - 使用 C++/CLI 从 Visual C/C++ 调用 Java 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9999913/