C++ call java.util.HashMap

//============================================================================
// Name        : usejavahashmap.cpp
// Author      : Onega
// Description : C++ call java.util.HashMap
// JNI Doc: http://download.oracle.com/javase/6/docs/technotes/guides/jni/index.html
// g++ -I/usr/lib/jvm/java-6-openjdk/include -I/usr/lib/jvm/java-6-openjdk/include/linux -O0 -g3 -Wall -c -fmessage-length=0 -v -MMD -MP -MF”src/usejavahashmap.d” -MT”src/usejavahashmap.d” -o “src/usejavahashmap.o” “../src/usejavahashmap.cpp”
// g++ -L/usr/lib/jvm/java-6-openjdk/jre/lib/i386/server/ -v -o “usejavahashmap”  ./src/usejavahashmap.o   -ljvm
// VC++2008:    cl /EHsc /IC:\jdk1.5.0_05\include /IC:\jdk1.5.0_05\include\win32 usejavahashmap.cpp /link /Debug /LIBPATH:C:\jdk1.5.0_05\lib  jvm.lib
//============================================================================
#include “jni.h”
#include <string>
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
JNIEnv *env;
jstring cstrtojstr(JNIEnv *env, const char* str)
{
int slen = strlen(str);
jchar* buffer = static_cast<jchar*>(alloca(sizeof(jchar)*(slen+1)));
int len = mbstowcs((wchar_t*)buffer, str, slen);
jstring js = env->NewString(buffer,len*sizeof(jchar));
return js;
}
std::ostream& operator<<(std::ostream& os, const jstring& js) {
if (env) {
size_t len = env->GetStringLength(js);
char* dest = static_cast<char*>(alloca(len*sizeof(jchar)+1));
memset(dest, 0, len*sizeof(jchar)+1);
jboolean copy = 1;
const jchar *src = env->GetStringChars(js, &copy);
size_t n = wcstombs(dest, reinterpret_cast<const wchar_t*>(src), len*sizeof(jchar));
env->ReleaseStringChars(js, src);
os << dest;
}
return os;
}

int main(int argc, char* argv[]) {
setlocale(LC_ALL,”zh_CN.GB18030″);
JavaVMInitArgs vm_args;
JavaVMOption options[3];
JavaVM *jvm;
options[0].optionString = “-Djava.compiler=NONE”;
options[1].optionString = “-Djava.class.path=.;”;
options[2].optionString = “-verbose:none”;
vm_args.version = JNI_VERSION_1_4;
vm_args.nOptions = 3;
vm_args.options = options;
vm_args.ignoreUnrecognized = JNI_TRUE;
int res = JNI_CreateJavaVM(&jvm, (void**) &env, &vm_args);
if (res < 0) {
cout << “Create JVM error ” << res << std::endl;
return res;
}
const char* hashmap_class_name = “java/util/HashMap”;
jclass clsHashMap = env->FindClass(hashmap_class_name);
jmethodID constructorID = env->GetMethodID
(clsHashMap, “<init>”, “()V”);
jobject jmapobj = env->NewObject(clsHashMap, constructorID);
//onega@ub-laptop:~$ javap -s java.util.HashMap
jmethodID map_put_mid = 0;
jmethodID size_method = 0;
jmethodID map_get_mid = 0;
map_put_mid = env->GetMethodID(clsHashMap, “put”,
“(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;”);
size_method = env->GetMethodID(clsHashMap, “size”, “()I”);
map_get_mid = env->GetMethodID(clsHashMap, “get”,
“(Ljava/lang/Object;)Ljava/lang/Object;”);
int size = (int) env->CallIntMethod(jmapobj, size_method);
cout << “map size is ” << size << std::endl;
jstring arg = cstrtojstr(env, “put1”);
jstring data = cstrtojstr(env, “data1”);
env->CallObjectMethod(jmapobj, map_put_mid, arg,
data);
size = (int) env->CallIntMethod(jmapobj, size_method);
cout << “map size is ” << size << std::endl;
jstring key = cstrtojstr(env, “put1”);
jstring map_value = (jstring)env->CallObjectMethod(jmapobj, map_get_mid, key);
if (map_value)
cout << “found value ” << map_value << std::endl;
else cout <<“not found value for key ” << key << std::endl;
jvm->DestroyJavaVM();
cout << argv[0] << ” exit!” << endl;
return 0;
}

Advertisements
This entry was posted in CPP, Java, JNI. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s