问题描述
我想写一个调用Java方法的C ++程序。
我试图从C ++调用Java函数。如此处所述
但我得到这个错误,而调试和无法处理它。
我使用Visual studio 2012.
这里是我的代码C ++代码。
#includestdafx.h
#include< jni.h> / * where everything is defined * /
使用namespace std;
int _tmain(int argc,_TCHAR * argv [])
{
JavaVM * jvm; / *表示Java VM * /
JNIEnv * env; / *指向本地方法接口的指针* /
JavaVMInitArgs vm_args; / * JDK / JRE 6 VM初始化参数* /
JavaVMOption * options = new JavaVMOption [1];
options [0] .optionString =-Djava.class.path = C:\\Users\\yv\\workspace\\JNI\\bin; //我的类在这个目录下。
vm_args.version = 0x00010006;
vm_args.nOptions = 1;
vm_args.options = options;
vm_args.ignoreUnrecognized = false;
/ *加载并初始化Java VM,在env * /
中返回JNI接口
*指针JNI_CreateJavaVM(& jvm,(void **)& env, & vm_args); //我有错误msg无法将参数2从'JNIEnv **'转换为'void **'所以添加(void **)如一些其他来源中所述
删除选项;
jvm-> DestroyJavaVM();
return 0;
}
我的comp版本上安装的Java是
C:\Users \yv> JAVA -version
Java版本1.7.0_17
Java(TM)SE运行时环境(版本1.7.0_17-b02)
Java HotSpot VM(构建23.7-b01,混合模式)
和我的java代码。
public class jniClass {
/ **
* @param args
* /
public static void main(String [] args){
// TODO自动生成方法存根
System.out.println(Hello,World!
System.out.println(Arguments sent to this program:);
if(args.length == 0){
System.out.println((None));
} else {
for(int i = 0; i System.out.print(args [i] +);
}
System.out.println();
}
}
}
我解决了这个问题。
错误:
未解析的外部符号_imp_JNI_CreateJavaVM @ 12引用
是因为jvm.lib没有链接。
方法。
使用LoadLibrary在运行时动态运行jvm.dll的第一个链接:
HINSTANCE hinstLib = LoadLibrary(TEXT(D:\\desired_jvm\\jre6\\jvm.dll));
typedef jint(JNICALL * PtrCreateJavaVM)(JavaVM **,void **,void *);
PtrCreateJavaVM ptrCreateJavaVM =(PtrCreateJavaVM)GetProcAddress(hinstLib,JNI_CreateJavaVM);
jint res = ptrCreateJavaVM(& jvm,(void **)& env,& vm_args);
第二个是将jvm.lib链接到您的项目。
右键单击项目 - >属性。
在属性对话框
在Linker-> Input-> AdditionalDependencies区域下添加jvm.lib。
在Linker-> General-> AdditionalLibraryDirectories下编写jvm.lib路径
I want to write a C++ program which calls Java method.
I am trying to invoke a Java function from C++. As described here
http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/invocation.html
but I get this error while debugging and can not handle it.I m using Visual studio 2012.Here is my code C++ code.
#include "stdafx.h"
#include <jni.h> /* where everything is defined */
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
JavaVM *jvm; /* denotes a Java VM */
JNIEnv *env; /* pointer to native method interface */
JavaVMInitArgs vm_args; /* JDK/JRE 6 VM initialization arguments */
JavaVMOption* options = new JavaVMOption[1];
options[0].optionString = "-Djava.class.path=C:\\Users\\yv\\workspace\\JNI\\bin"; // my class is under this directory.
vm_args.version = 0x00010006;
vm_args.nOptions = 1;
vm_args.options = options;
vm_args.ignoreUnrecognized = false;
/* load and initialize a Java VM, return a JNI interface
* pointer in env */
JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args); // I got err msg "cannot convert parameter 2 from 'JNIEnv **' to 'void **' " so added (void **) as described in some other sources
delete options;
jvm->DestroyJavaVM();
return 0;
}
Java installed on my comp version isC:\Users\yv>JAVA -versionjava version "1.7.0_17"Java(TM) SE Runtime Environment (build 1.7.0_17-b02)Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)
and my java code in case of need.
public class jniClass {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello, World!");
System.out.println("Arguments sent to this program:");
if (args.length == 0) {
System.out.println("(None)");
} else {
for (int i=0; i<args.length; i++) {
System.out.print(args[i] + " ");
}
System.out.println();
}
}
}
I solved the problem.The error:
unresolved external symbol _imp_JNI_CreateJavaVM@12 referenced
is because the jvm.lib is not being linked with.
It can be solved by two ways.
First link to jvm.dll dynamically at run time, by using LoadLibrary:
HINSTANCE hinstLib = LoadLibrary(TEXT("D:\\desired_jvm\\jre6\\bin\\client\\jvm.dll"));
typedef jint (JNICALL *PtrCreateJavaVM)(JavaVM **, void **, void *);
PtrCreateJavaVM ptrCreateJavaVM = (PtrCreateJavaVM)GetProcAddress(hinstLib,"JNI_CreateJavaVM");
jint res = ptrCreateJavaVM(&jvm, (void**)&env, &vm_args);
Second is link the jvm.lib to your project.
Right click on the project -> properties.
On the Properties dialog box
add jvm.lib under Linker->Input->AdditionalDependencies area.
And write jvm.lib path under Linker->General->AdditionalLibraryDirectories
这篇关于未解析的外部符号__imp__JNI_CreateJavaVM @ 12引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!