Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

java - unresolved external symbol __imp__JNI_CreateJavaVM@12 referenced

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 is C:Usersyv>JAVA -version java 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();
    }
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I solved the error:

unresolved external symbol _imp_JNI_CreateJavaVM@12 referenced

by installing the correct version of JDK for the application configuration. I installed 64-bit version of JDK on Windows 7 (64-bit) machine. However, my application was 32-bit. I un-installed the 64-bit JDK version and installed the 32-bit. Afterwards there was no linking error.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...