This discussion is archived
5 Replies Latest reply: Jan 22, 2013 8:07 PM by 986268 RSS

main method..

986268 Newbie
Currently Being Moderated
How JVM starts the main method?
  • 1. Re: main method..
    gimbal2 Guru
    Currently Being Moderated
    - create JVM instance
    - load class(es)
    - locate public static main(String[]) method
    - make JVM instance execute it

    Its all in the source of the java wrapper executable if you really must know. Me - I wouldn't care about it.
  • 2. Re: main method..
    986268 Newbie
    Currently Being Moderated
    most of the java books just tell that jvm starts the main method....some wrote that user thread (called main)is created then from that thread just main method is invoked. Is it possible to know the internal mechanism of invoking the main method..
  • 3. Re: main method..
    jduprez Pro
    Currently Being Moderated
    983265 wrote:
    most of the java books just tell that jvm starts the main method....
    some wrote that user thread (called main)is created then from that thread just main method is invoked
    Both description seems plausible. Just different levels of details. Neither is complete (a previous answer mentioned loading and initializing classes), it's merely the appropriate level of abstraction for the majority of us that are essentially interested in having their main() (and all other application code) executed.
    Is it possible to know the internal mechanism of invoking the main method...
    There is probably no such thing as the mechanism, as different JVM implementations could probably choose different approaches.
    If you need to know the exact specifications that all JVMs must comply with, look for the JVM specifications, in particular this chapter http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-5.html#jvms-5.2 (which mentions the freedom of implementation choices)

    But again, why do you ask?
    "Just out of curiosity" is a valid answer, but "because my program will sorely depend on this mechanism" would sound suspicious.

    Brgds,

    J.
  • 4. Re: main method..
    baftos Expert
    Currently Being Moderated
    This explains how an arbitrary application can invoke the java VM. This is also how java itself invokes it, probably with a lot of detail omitted.
    http://docs.oracle.com/javase/1.4.2/docs/guide/jni/spec/invocation.html
  • 5. Re: main method..
    986268 Newbie
    Currently Being Moderated
    Just for the curiosity... nothing else..
    Anyway thanx for the links..

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points