使用jni.h在C++中编译java方法时出现构建错误
Build error when compiling java methods in C++ using jni.h
我正在Visual Studio C++环境中尝试从Java类运行一些方法。我得到了一个与CreateJavaVM相关的错误。
你能帮我找到解决办法吗?
我做了以下步骤。
步骤1:JDK 1.6安装在以下路径下:C:\Program Files\Java下面有两个子目录:jdk1.6.0_45,jre6
步骤2:编写一个简单的java程序。
public class Sample2
{
public static int intMethod(int n)
{
return n*n;
}
public static boolean booleanMethod(boolean bool)
{
return !bool;
}
}
步骤3:编译java代码:
javac Sample2.java
步骤4:创建Visual Studio C++程序。Visual C++CLR控制台应用程序。
第5步:添加其他依赖项。(jvm.lib和jvm.dll)a) 选择项目->属性->链接器->输入->其他依赖项:jvm.libb) 选择项目->属性->链接器->输入->延迟加载的Dlls:jvm.dll
步骤6:添加包含目录a) 选择项目->属性->配置属性->C/C++->常规->其他包含目录:C:\Program Files\Java\jdk1.6.0_45\lib;C: \Program Files\Java\jdk1.6.0_45\include\win32;C: \Program Files\Java\jdk1.6.0_45\包括
步骤7:编写C++代码以运行java方法。
#include "stdafx.h"
#include "jni.h"
#include <jni_md.h>
using namespace System;
int main(array<System::String ^> ^args)
{
JavaVM *jvm; /* denotes a Java VM */
JNIEnv *env; /* pointer to native method interface */
jint square;
jboolean not;
JavaVMInitArgs vm_args; /* JDK/JRE 6 VM initialization arguments */
JavaVMOption *options = new JavaVMOption[1];
options[0].optionString = "-Djava.class.path=C:\JavaCode";
vm_args.version = JNI_VERSION_1_6;
vm_args.options = options;
vm_args.nOptions = 1;
vm_args.ignoreUnrecognized = false;
int res = JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args);
jclass cls = env->FindClass("Sample2");
jmethodID mid = env->GetStaticMethodID(cls, "staticInt", "(I)I");
env->CallStaticVoidMethod(cls, mid,10);
if(cls !=0)
{
mid = env->GetStaticMethodID(cls,"intMethod","(I)I");
if(mid !=0)
{
square = env->CallStaticIntMethod(cls, mid, 5);
printf("Result of intMethod: %dn", square);
}
mid = env->GetStaticMethodID(cls, "booleanMethod", "(Z)Z");
if(mid !=0)
{
not = env->CallStaticBooleanMethod(cls, mid, 1);
printf("Result of booleanMethod: %dn", not);
}
}
jvm->DestroyJavaVM();
Console::Read();
return 0;
}
步骤8:当我构建项目时,我得到了以下错误:
1>------ Build started: Project: 1, Configuration: Debug Win32 ------
1>LINK : warning LNK4199: /DELAYLOAD:jvm.dll ignored; no imports found from jvm.dll
1>1.obj : warning LNK4248: unresolved typeref token (0100000F) for '_jmethodID'; image may not run
1>1.obj : error LNK2028: unresolved token (0A000016) "extern "C" long __stdcall JNI_CreateJavaVM(struct JavaVM_ * *,void * *,void *)" (?JNI_CreateJavaVM@@$$J212YGJPAPAUJavaVM_@@PAPAXPAX@Z) referenced in function "int __clrcall main(cli::array<class System::String ^ >^)" (?main@@$$HYMHP$01AP$AAVString@System@@@Z)
1>1.obj : error LNK2019: unresolved external symbol "extern "C" long __stdcall JNI_CreateJavaVM(struct JavaVM_ * *,void * *,void *)" (?JNI_CreateJavaVM@@$$J212YGJPAPAUJavaVM_@@PAPAXPAX@Z) referenced in function "int __clrcall main(cli::array<class System::String ^ >^)" (?main@@$$HYMHP$01AP$AAVString@System@@@Z)
1>C:UserstveluppillaiDesktopTest11Debug1.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
有人能帮我解决这个错误吗?
JNI_CreateJavaVM
是在jni.h
中定义的,所以我猜测您的链接器找不到jni.h
。实际上,我把那个文件和jni_md.h
和我的源代码放在了我的项目目录中。
请注意,jni.h
包括jni_md.h
,因此在源代码中包含#include <jni_md.h>
是多余的。
所以,我解决了这个错误。以下是正在使用的C++代码。
// 1.cpp : main project file.
#include "stdafx.h"
#include "jni.h"
#include <windows.h>
using namespace System;
int CallJava()
{
JavaVM *jvm; /* denotes a Java VM */
JNIEnv *env; /* pointer to native method interface */
jint square;
jboolean not;
JavaVMInitArgs vm_args; /* JDK/JRE 6 VM initialization arguments */
JavaVMOption *options = new JavaVMOption[1];
options[0].optionString = "-Djava.class.path=C:\JavaCode";
vm_args.version = JNI_VERSION_1_6;
vm_args.options = options;
vm_args.nOptions = 1;
vm_args.ignoreUnrecognized = false;
HINSTANCE hinstLib;
hinstLib = LoadLibrary(TEXT("C:\Program Files\Java\jdk1.6.0_45\jre\bin\server\jvm.dll"));
if(hinstLib==0)
{
printf("Error");
}
if(hinstLib!= NULL)
{
typedef jint (JNICALL *PtrCreateJavaVM)(JavaVM **, void **, void *);
PtrCreateJavaVM ptrCreateJavaVM = (PtrCreateJavaVM)GetProcAddress(hinstLib,"JNI_CreateJavaVM");
int res = ptrCreateJavaVM(&jvm, (void**)&env, &vm_args);
jclass cls = env->FindClass("Sample2");
jmethodID mid;
if(cls !=0)
{
mid = env->GetStaticMethodID(cls,"intMethod","(I)I");
if(mid !=0)
{
square = env->CallStaticIntMethod(cls, mid, 5);
printf("Result of intMethod: %dn", square);
}
mid = env->GetStaticMethodID(cls, "booleanMethod", "(Z)Z");
if(mid !=0)
{
not = env->CallStaticBooleanMethod(cls, mid, 1);
printf("Result of booleanMethod: %dn", not);
}
}
jvm->DestroyJavaVM();
}
else
{
printf("Library is NULL");
}
Console::Read();
return 0;
}
int main(array<System::String ^> ^args)
{
CallJava();
Console::Read();
return 0;
}
- C++:正在检查LinkedList中的回文-递归方法-错误
- C++数组队列实现方法错误
- 方法错误"not all control paths return a value"和方法不返回值
- OpenGL 顶点缓冲区类重定义和模板方法错误
- 如何在 VC++ 中通过引用传递另一个对象的方法(错误 C2664)
- 复数程序编译时的计算方法错误
- CMake 构建错误 - 虚拟方法错误缺少 vtable
- 接受 lambda 作为参数的类方法 - 错误消息
- c++程序方法错误visualstudio
- OpenCV "findContours"方法错误
- std::线程类方法错误
- 基类没有新方法错误
- C++派生类继承方法错误
- 指向类方法错误c++11的指针数组
- 使用jni调用方法:错误的jni oop参数
- QVector.push_back方法错误
- 对静态方法错误的未定义引用
- Friend方法错误
- async_write方法错误
- 重写方法错误C++