+/* vm_run **********************************************************************
+
+ Runs the main-method of the passed class.
+
+*******************************************************************************/
+
+void vm_run(JavaVM *vm, JavaVMInitArgs *vm_args)
+{
+ utf *mainutf;
+ classinfo *mainclass;
+ java_handle_t *e;
+ methodinfo *m;
+ java_handle_objectarray_t *oa;
+ s4 oalength;
+ utf *u;
+ java_handle_t *s;
+ s4 status;
+ s4 i;
+
+#if !defined(NDEBUG)
+ if (compileall) {
+ vm_compile_all();
+ return;
+ }
+
+ if (opt_method != NULL) {
+ vm_compile_method();
+ return;
+ }
+#endif /* !defined(NDEBUG) */
+
+ /* should we run the main-method? */
+
+ if (mainstring == NULL)
+ usage();
+
+ /* set return value to OK */
+
+ status = 0;
+
+ if (opt_jar == true) {
+ /* open jar file with java.util.jar.JarFile */
+
+ mainstring = vm_get_mainclass_from_jar(mainstring);
+
+ if (mainstring == NULL)
+ vm_exit(1);
+ }
+
+ /* load the main class */
+
+ mainutf = utf_new_char(mainstring);
+
+#if defined(ENABLE_JAVAME_CLDC1_1)
+ mainclass = load_class_bootstrap(mainutf);
+#else
+ mainclass = load_class_from_sysloader(mainutf);
+#endif
+
+ /* error loading class */
+
+ e = exceptions_get_and_clear_exception();
+
+ if ((e != NULL) || (mainclass == NULL)) {
+ exceptions_throw_noclassdeffounderror_cause(e);
+ exceptions_print_stacktrace();
+ vm_exit(1);
+ }
+
+ if (!link_class(mainclass)) {
+ exceptions_print_stacktrace();
+ vm_exit(1);
+ }
+
+ /* find the `main' method of the main class */
+
+ m = class_resolveclassmethod(mainclass,
+ utf_new_char("main"),
+ utf_new_char("([Ljava/lang/String;)V"),
+ class_java_lang_Object,
+ false);
+
+ if (exceptions_get_exception()) {
+ exceptions_print_stacktrace();
+ vm_exit(1);
+ }
+
+ /* there is no main method or it isn't static */
+
+ if ((m == NULL) || !(m->flags & ACC_STATIC)) {
+ exceptions_clear_exception();
+ exceptions_throw_nosuchmethoderror(mainclass,
+ utf_new_char("main"),
+ utf_new_char("([Ljava/lang/String;)V"));
+
+ exceptions_print_stacktrace();
+ vm_exit(1);
+ }
+
+ /* build argument array */
+
+ oalength = vm_args->nOptions - opt_index;
+
+ oa = builtin_anewarray(oalength, class_java_lang_String);
+
+ for (i = 0; i < oalength; i++) {
+ u = utf_new_char(vm_args->options[opt_index + i].optionString);
+ s = javastring_new(u);
+
+ array_objectarray_element_set(oa, i, s);
+ }
+
+#ifdef TYPEINFO_DEBUG_TEST
+ /* test the typeinfo system */
+ typeinfo_test();
+#endif
+ /*class_showmethods(currentThread->group->header.vftbl->class); */
+
+#if defined(ENABLE_JVMTI)
+ jvmti_set_phase(JVMTI_PHASE_LIVE);
+#endif
+
+ /* set ThreadMXBean variables */
+
+ _Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount++;
+ _Jv_jvm->java_lang_management_ThreadMXBean_TotalStartedThreadCount++;
+
+ if (_Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount >
+ _Jv_jvm->java_lang_management_ThreadMXBean_PeakThreadCount)
+ _Jv_jvm->java_lang_management_ThreadMXBean_PeakThreadCount =
+ _Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount;
+
+ /* start the main thread */
+
+ (void) vm_call_method(m, NULL, oa);
+
+ /* exception occurred? */
+
+ if (exceptions_get_exception()) {
+ exceptions_print_stacktrace();
+ status = 1;
+ }
+
+ /* unload the JavaVM */
+
+ (void) vm_destroy(vm);
+
+ /* and exit */
+
+ vm_exit(status);
+}
+
+