#include // we want to use the CNI #include #include // we want to use some java classes #include #include #include #include #include #include #include #include int main(int argc, char **argv) { if (argc<2) { std::cerr << "Usage: " << argv[0] << ": CLASS [ARGS]\n"; std::cerr << "to interpret java bytecode\n"; return 1; } using namespace java::lang; try{ // create the virtual machine JvCreateJavaVM(NULL); JvAttachCurrentThread(NULL, NULL); // create a java string from a c string String *className = JvNewStringLatin1("single.Sigclass"); JvInitClass(&Sigclass::class$); //JvInitClass(&System::class$); JvInitClass(&Class::class$); // 1) try to find this class // first it will be searched for a shared library containg native code matching this class // if no shared library is found the byte-code is searched // either in a className.class file or in the CLASSPATH Class *cl = Class::forName(className); // 2) now we use the reflection API to find the static main(String []) method of this class String *methodName = JvNewStringLatin1("main"); // we need an Class[] with Class[0]=String[].getClass() Class *stringArrayClass=JvNewObjectArray(0,(new String())->getClass(),NULL)->getClass(); typedef JArray ClassArray; ClassArray *parameterTypes = (ClassArray *)JvNewObjectArray(1, (new Class())->getClass(),stringArrayClass); JvInitClass(&Class::class$); JArray* m=cl->getMethods(); reflect::Method **tl =(reflect::Method **)elements(m); //reflect::Method* m=cl->getMethods(); // check that this is a static method // todo: if there is a non-static main we could instantiate an object and run this one if (!reflect::Modifier::isStatic(tl[0]->getModifiers())) { std::cerr << "Did find main but it isn't static !\n"; return 1; } // 3) and now we invoke the method passing in a String[] with our remaining arguments // create the argument vector typedef JArray StringArray; StringArray *stringArray = (StringArray *)JvNewObjectArray(argc-2,(new String())->getClass(),NULL); String **s=(String **)elements(stringArray); for (int i=2;iinvoke(NULL,parameters); JvDetachCurrentThread(); }catch (Throwable *t){ System::err->println(JvNewStringLatin1("Unhandled Java exception:")); t->printStackTrace(); } }