I was looking through some Java source and noticed that the main method wasn’t defined.
How does Java compile source code without knowing where to start?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
mainmethod is only used when the Java Virtual Machine is executing your code. Code cannot be executed without amainmethod but it can still be compiled.When compiling code, you usually specify a set of files on the command line e.g.
The Java compiler (
javac) examines each class you passed to it and compiles it into a .class file.One reason Java source code may be missing a
mainmethod is because it is designed to be used as a library, instead of being executed.Something you may find interesting: although the source code compiled by the Java compiler does not need a
mainmethod, the source code for the Java compiler itself does have amainmethod.