首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于C++调用java的JNI有关问题

2012-02-09 
关于C++调用java的JNI问题肯定结帖!!!#includejni.hintmain(intargc,char*argv[]){intresJavaVM*jvmJN

关于C++调用java的JNI问题
肯定结帖!!!

#include   <jni.h>


int   main(int   argc,   char*   argv[])
{

int   res;
JavaVM   *jvm;
JNIEnv   *env;
JavaVMInitArgs   vm_args;
JavaVMOption   options[3];
vm_args.version=JNI_VERSION_1_4;
//这个字段必须设置为该值
/*设置初始化参数*/
options[0].optionString   =   "-Djava.compiler=NONE ";
options[1].optionString   =   "-Djava.class.path=. ";
options[2].optionString   =   "-verbose:jni ";
//用于跟踪运行时的信息
/*版本号设置不能漏*/
vm_args.version   =   JNI_VERSION_1_4;
vm_args.nOptions   =   3;
vm_args.options   =   options;
vm_args.ignoreUnrecognized   =   JNI_TRUE;
res   =JNI_CreateJavaVM(&jvm,(void**)&env,&vm_args);
if   (res   <   0)
{
fprintf(stderr, "Can 't   create   Java   VM\n ");
exit(1);
}
jvm-> DestroyJavaVM();
fprintf(stdout, "Java   VM   destory.\n ");
}
这是一段c++初始化jvm的代码,我用的是vc+6.0在project--setting--link里开始没设置jvm.lib时候报错找不到很多函数,后来我加进去了。程序编译没有错,可是运行的时候照不到jvm.dll,把它拷到项目下能编译过,运行也不报错,可是   JNI_CreateJavaVM不成功,返回的总是负数,把jvm.dll拷贝过来不是办法,应该有标准的方法。哪位高人把以上问题指点一下?不胜感激!

[解决办法]
参考这个例子: 【Ref】

#include "stdafx.h "
#include "windows.h "
#include <jni.h>

int main() {

JavaVMOption options[3];
JavaVMInitArgs vm_args;
JavaVM *jvm;
JNIEnv *env;
int status;

// constructor

printf( "Starting JVM creation.\n ");

// sets System Properties specific to VM
options[0].optionString = "-Djava.compiler=NONE "; options[1].optionString = "-Djava.class.path=. ";
options[2].optionString = "-verbose:jni ";

// setting JVM options.
vm_args.version=JNI_VERSION_1_4;
vm_args.options = options;
vm_args.nOptions = 3;
vm_args.ignoreUnrecognized = JNI_FALSE;

printf( "\nvm_args.version is : %d ", vm_args.version);
printf( "\nvm_args.options is : %s ", vm_args.options);
printf( "\nvm_args.nOptions is : %d ", vm_args.nOptions);

// printf( " env is : %s\n ", *env); --------(1)
// printf( " jvm is : %s\n ", *jvm); ---------(2)
// printf( " vm_args is : %s\n ", vm_args); --------(3)


// creates JVM instance
status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
if (status == JNI_ERR)
{
// handle it
}
else
{
printf( "\nJVM is created successfully.\n\n ");
}
//
jclass mainClass = env-> FindClass( "jni/test/Demo ");


if (mainClass == NULL)
{
printf( "Unable to load main class. ");
}
else
{
printf( "find class successfully! ");
}


jmethodID mid=env-> GetMethodID(mainClass, "show ", "()V ");
jmethodID ctor = env-> GetMethodID(mainClass, " <init> ", "()V ");


jobject obj = env-> NewObject(mainClass, ctor);


if (mid == 0)
{
printf( "\nUnable to find the method . ");
} else {

printf( "\nGot method . ");



//jfieldID jID = env-> GetFieldID(mainClass, "msg ", "Ljava/lang/String; ");
env-> CallVoidMethod(obj,mid);
}


//Destroy the JVM instance
jvm-> DestroyJavaVM();

}
已经搞定了,java如下:
package jni.test;
/**
* 该类是为了演示JNI如何访问各种对象属性等
* @author liudong
*/
import java.io.*;
public class Demo {
//用于演示如何访问静态的基本类型属性
public static int COUNT = 8;
//演示对象型属性
public String msg = "successfully! ";
private int[] counts;
public Demo() {
this( "缺省构造函数 ");
}
/**
* 演示如何访问构造器
*/
public Demo(String msg) {
System.out.println( " <init> : " + msg);
this.msg = msg;
this.counts = null;
}
/**
* 该方法演示如何访问一个访问以及中文字符的处理
*/
public int getMessage() {
System.out.println( "\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= work ");
return 1;
}
/**
* 演示数组对象的访问
*/
public int[] getCounts() {
return counts;
}
/**
* 演示如何构造一个数组对象
*/
public void setCounts(int[] counts) {
this.counts = counts;
}
/**
* 演示异常的捕捉
*/
public void throwExcp() throws IllegalAccessException {
throw new IllegalAccessException( "exception occur. ");
}

public void show()
{
System.out.println( "\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= work ");
/*

File fTest = new File( "java ");
try
{
fTest.mkdir();

}
catch(Exception e)
{
e.printStackTrace();
}*/
}
}

热点排行