Yes, other languages which run on the JVM create objects or modules (which are also objects) and run them. For example, the Fortress language 'Hello world' looks like
Component HelloWorld
Export Executable
run(args) = print "Hello, world!"
end
or, without the args:
Component HelloWorld
Export Executable
run() = print "Hello, world!"
end
Java is a bit more pragmatic than a pure OO language, having static methods and fields and primitive types. Its static main method is closer to C's main function. You would have to ask Gosling as to why he chose that convention.
The code to launch the JVM is fairly simple - this example creates a JVM, creates an object and calls its run
method with the command line arguments - making the startup function (new main.HelloWorld()).run(args)
rather than main.HelloWorld.main(args)
:
#include
#include
JNIEnv* create_vm() {
JavaVM* jvm;
JNIEnv* env;
JavaVMInitArgs args;
JavaVMOption options[1];
args.version = JNI_VERSION_1_2;
args.nOptions = 1;
options[0].optionString = "-Djava.class.path=C:\\java_main\\classes";
args.options = options;
args.ignoreUnrecognized = JNI_TRUE;
JNI_CreateJavaVM(&jvm, (void **)&env, &args);
return env;
}
int invoke_class(JNIEnv* env, int argc, char **argv) {
jclass helloWorldClass;
helloWorldClass = env->FindClass("main/HelloWorld");
if (helloWorldClass == 0)
return 1;
jmethodID constructorMethod = env->GetMethodID(helloWorldClass, "", "()V");
jobject object = env->NewObject(helloWorldClass, constructorMethod);
if (object == 0)
return 1;
jobjectArray applicationArgs = env->NewObjectArray(argc, env->FindClass("java/lang/String"), NULL);
for (int index = 0; index < argc; ++index) {
jstring arg = env->NewStringUTF(argv[index]);
env->SetObjectArrayElement(applicationArgs, index, arg);
}
jmethodID runMethod = env->GetMethodID(helloWorldClass, "run", "([Ljava/lang/String;)V");
env->CallVoidMethod(object, runMethod, applicationArgs);
return 0;
}
int main(int argc, char **argv) {
JNIEnv* env = create_vm();
return invoke_class( env, argc, argv );
}