Reputation: 1161
Pardon me for asking this silly question. Where can i find java main method definition in java source? it is not in object class or system class? so how and where is it defined exactly?
public static void main(String[] args) {}
Upvotes: 1
Views: 1451
Reputation: 718758
Where is Java Main method defined in source?
It is declared in a class. Conventionally, it is a top-level (i.e. non-nested) public
class, but that is not a requirement. (A non-public class will work, and I think a static nested class will work too.)
How do you find the main
method?
Use grep
or similar to search the source code of your application.
Use your IDE's method search capability.
Read the application's user documentation or launch script.
Look for the main
method in the index of the application's javadoc.
How does the java
command find it?
It doesn't! You specify the fully qualified class name of the class containing the main
method you want to use on the java
command line. Alternatively, you can set the Main-Class attribute in a JAR file's manifest so that the user doesn't need to know the class name.
UPDATE - If you are looking for the code in the OpenJDK source tree that loads the entrypoint class, finds the main method and invokes it, it is all in "jdk8u/jdk/src/share/bin/java.c". Happy reading.
Upvotes: 3
Reputation: 4411
main method is the entry point of an application in java. All the java classes are packaged as libraries which will be used in any application. So class files are used as references instead of separte executable. You can't execute the java source code separately because there won't be any main method definition in java source code.
Upvotes: 0
Reputation: 73528
It's not defined anywhere as code (in the standard libraries).
The JVM
expects to find it if you're running a class, and if it's not found you get an error. Therefore it's up to you to create a public static void main(String[] args)
method if you want to run your class.
Upvotes: 0