-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
92 lines (71 loc) · 2.15 KB
/
Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "IO_JAVAConfig.h"
#include <dlfcn.h>
#include "io_java.h"
#include <memory.h>
#include <uv.h>
#include "Env.h"
#include "Env.cpp"
#include <JavaVM/jni.h>
using namespace io_java;
int loadLib(JNIEnv* jniEnv, const char* path);
int main (int argc, char *argv[])
{
Env* env = Env::getOrCreate();
JavaVM* vm = env->getVM();
JNIEnv* jniEnv = env->getJNIEnv();
if(argc < 2) {
printf("no main class specified.");
return -1;
}
int loadResult = loadLib(jniEnv,"/Users/kenvi/code/study/io.java/build/Debug/libjavaio.dylib");
if(loadResult < 0) {
return loadResult;
}
jclass cls = jniEnv->FindClass(argv[1]);
if (jniEnv->ExceptionOccurred()) {
printf("exception");
jniEnv->ExceptionDescribe();
return -1;
}
jmethodID mid = jniEnv->GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V");
if (jniEnv->ExceptionOccurred()) {
printf("exception");
jniEnv->ExceptionDescribe();
return -1;
}
jniEnv->CallStaticVoidMethod(cls, mid, NULL);
if (jniEnv->ExceptionOccurred()) {
jniEnv->ExceptionDescribe();
printf("exception");
return -2;
}
uv_loop_t* loop = env->getEventLoop();
uv_run(loop, UV_RUN_ONCE);
printf("hello world from io.java");
vm->DestroyJavaVM();
return 0;
}
int loadLib(JNIEnv* jniEnv, const char* path) {
jclass cls = jniEnv->FindClass("io/java/lang/System");
if (jniEnv->ExceptionOccurred()) {
printf("exception");
jniEnv->ExceptionDescribe();
return -1;
}
jmethodID mid = jniEnv->GetStaticMethodID(cls, "load", "()V");
// jmethodID mid = jniEnv->GetStaticMethodID(cls, "load", "(Ljava/lang/String;)V");
if (jniEnv->ExceptionOccurred()) {
printf("exception");
jniEnv->ExceptionDescribe();
return -1;
}
jniEnv->CallStaticVoidMethod(cls, mid,jniEnv->NewStringUTF(path));
if (jniEnv->ExceptionOccurred()) {
printf("exception");
jniEnv->ExceptionDescribe();
}
return 0;
}