11 * Spouští JVM z C++, volá statickou metodu ze standardní knihovny (System.currentTimeMillis()) a main() metodu programu SQL-DK
17 int main(int argc, char** argv) {
19 setlocale(LC_ALL, "");
24 JavaVMOption options[1];
25 char * home = getenv("HOME");
28 classPath.append("-Djava.class.path=");
29 classPath.append(home);
30 classPath.append("/projekty/SQL-DK/sql-dk/java/sql-dk/dist/sql-dk.jar");
31 classPath.append(":");
32 classPath.append(home);
33 classPath.append("/projekty/SQL-DK/sql-dk/java/jdbc-loopback-driver/dist/jdbc-loopback-driver.jar");
35 wprintf(L"classPath: %s\n", classPath.c_str());
37 options[0].optionString = new char[classPath.length() + 1];
38 copy(classPath.begin(), classPath.end(), options[0].optionString);
40 JavaVMInitArgs vm_args;
41 vm_args.version = JNI_VERSION_1_8;
43 vm_args.options = options;
44 vm_args.ignoreUnrecognized = false;
46 JNI_CreateJavaVM(&jvm, (void**) &env, &vm_args);
48 string className = "java/lang/System";
50 jclass cls = env->FindClass(className.c_str());
52 string methodName = "currentTimeMillis";
53 jmethodID mid = env->GetStaticMethodID(cls, methodName.c_str(), "()J");
56 jlong time = env->CallStaticLongMethod(cls, mid);
57 wprintf(L"Aktuální čas Javy: %d\n", time);
60 wprintf(L"Metoda nebyla nalezena: %s\n", methodName.c_str());
63 wprintf(L"Třída nebyla nalezena: %s\n", className.c_str());
66 delete cls; // TODO: jak čistit?
68 className = "info/globalcode/sql/dk/CLIStarter";
69 cls = env->FindClass(className.c_str());
71 string methodName = "main";
72 jmethodID mid = env->GetStaticMethodID(cls, methodName.c_str(), "([Ljava/lang/String;)V");
75 //jstring arg = env->NewStringUTF("--list-databases");
76 //jarray args = env->NewObjectArray(1, env->FindClass("java/lang/String"), arg);
78 jobjectArray args = env->NewObjectArray(10, env->FindClass("java/lang/String"), NULL);
80 env->SetObjectArrayElement(args, a++, env->NewStringUTF("--db"));
81 env->SetObjectArrayElement(args, a++, env->NewStringUTF("loopback"));
82 //env->SetObjectArrayElement(args, a++, env->NewStringUTF("--sql-in")); // vyžaduje (libovolná) data na standardním vstupu
83 env->SetObjectArrayElement(args, a++, env->NewStringUTF("--sql"));
84 env->SetObjectArrayElement(args, a++, env->NewStringUTF("…"));
85 env->SetObjectArrayElement(args, a++, env->NewStringUTF("--data"));
86 env->SetObjectArrayElement(args, a++, env->NewStringUTF("2"));
87 env->SetObjectArrayElement(args, a++, env->NewStringUTF("a"));
88 env->SetObjectArrayElement(args, a++, env->NewStringUTF("b"));
89 env->SetObjectArrayElement(args, a++, env->NewStringUTF("c"));
90 env->SetObjectArrayElement(args, a++, env->NewStringUTF("d"));
93 for (int i = 0; i < 10; i++) {
94 env->CallStaticVoidMethod(cls, mid, args);
95 // FIXME: nefuguje – JVM ukončí proces → vlákna/procesy
100 wprintf(L"Metoda nebyla nalezena: %s\n", methodName.c_str());
103 wprintf(L"Třída nebyla nalezena: %s\n", className.c_str());
106 jvm->DestroyJavaVM();