Skip to content

Instantly share code, notes, and snippets.

@enukane
Created Jan 16, 2010
Embed
What would you like to do?
#include<stdio.h>
#include<jni.h>
#include<string.h>
/*
* here we expect Hoge class in my.hoge package
*
*/
int main(int argc, char** argv)
{
JNIEnv *jnienv;
JavaVM *javavm;
JavaVMInitArgs vm_args;
JavaVMOption options[1];
options[0].optionString = "-Djava.class.path=.";
vm_args.version = JNI_VERSION_1_2;
vm_args.options = options;
vm_args.nOptions = 1;
vm_args.ignoreUnrecognized = true;
printf("Java VM generate\n");
int result = JNI_CreateJavaVM(&javavm, (void **)&jnienv, &vm_args);
puts("create jvm");
if(result != 0){
printf("jvm failed to create : %d\n", result);
return 1;
}
puts("searching class");
jclass cls = jnienv->FindClass("my/hoge/Hoge");
if(cls == 0){
puts("failed to find class");
return 1;
}
puts("find constructor method");
jmethodID cns = jnienv->GetMethodID(cls, "<init>", "()V");
if(cns == NULL){
puts("constructor method find failed");
return 1;
}
puts("instantialize");
jobject obj = jnienv->NewObject(cls, cns);
if(!obj){
puts("failed to new");
return 1;
}
// printHoge
puts("searching method: printHoge");
jmethodID printHogeId = jnienv->GetMethodID(cls, "printHoge", "()V");
if(printHogeId == NULL){
puts("failed to find printHoget");
return 1;
}
puts("----method calling");
jnienv->CallVoidMethod(obj, printHogeId);
puts("----method called");
// add
puts("searching method: add");
jmethodID addId = jnienv->GetMethodID(cls, "add", "(II)I");
if(!addId){
puts("failed to find add");
return 1;
}
puts("----call");
int addResult = (int)jnienv->CallIntMethod(obj, addId, 10, 100);
printf("----called : result = %d\n", addResult);
// getHoge
puts("searching method: getHoge");
jmethodID getHogeId = jnienv->GetMethodID(cls, "getHoge", "()Ljava/lang/String;");
if(!getHogeId){
puts("failed to find getHoge");
return 1;
}
puts("----call");
jstring jstr1 = (jstring)jnienv->CallObjectMethod(obj, getHogeId);
const char* str1 = jnienv->GetStringUTFChars(jstr1, 0);
char* resStr1 = strdup(str1);
jnienv->ReleaseStringUTFChars(jstr1, str1);
printf("----called : result = %s\n", resStr1);
// getMsg
puts("searching method: geMsg");
jmethodID getMsgId = jnienv->GetMethodID(cls, "getMsg", "(Ljava/lang/String;)Ljava/lang/String;");
if(!getMsgId){
puts("failed to find getMsg");
return 1;
}
puts("----call");
jstring jstr2 = (jstring)jnienv->CallObjectMethod(obj, getMsgId, jnienv->NewStringUTF("Hell to the Sack code!"));
const char* str2 = jnienv->GetStringUTFChars(jstr2, 0);
char* resStr2 = strdup(str2);
jnienv->ReleaseStringUTFChars(jstr2, str2);
printf("----called : result = %s\n", resStr2);
// destroy JVM
result = javavm->DestroyJavaVM();
if(result){
puts("Failed to destroy jvm");
return 1;
}
puts("Safely destroyed jvm");
return 0;
}
package my.hoge;
public class Hoge {
public Hoge()
{
}
public void printHoge(){
System.out.println("Hoge!");
}
public String getHoge(){
return new String("Hoge");
}
public int add(int i, int j)
{
return i + j;
}
public String getMsg(String msg){
return new String("Hoget is " + msg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment