Who is calling the main function in java?

public static void main(String[] args) { boolean t=true; System.out.println("Before return"); if(t) return; System.out.println("not execute"); } 

In the above code, when return used, it should return to the function that calls the main function. Who exactly calls the main function?

+6
java
source share
7 answers

Java classes run in a wider context (a specific JVM, as others have pointed out). Below are some features:

In all cases, the main() method is the canonical entry point for executing the code specified by a particular class. From the docs on java JVM:

DESCRIPTION

The java tool launches a Java application. He does this by starting the Java runtime, loading the specified class and calling this main class method. The method declaration should look like this:

  public static void main(String args[]) 

The method must be declared public and static, it must not return any value, and must accept a String array as a parameter. By default, the first argument without a parameter is the name of the class to call. The fully qualified class name must be used. If the -jar parameter is specified, the first argument without the parameter is the name of the JAR archive containing the class and resource files for the application, and the launch class is indicated by the header of the Main-Class manifest.

The Java runtime looks for the launch class and other classes in three sets of locations: the bootstrap class path, installed extensions, and the user class path.

Arguments without a parameter after the class name or JAR file name are passed to the main function.

The javaw command is identical to java, except that there is no console window associated with javaw. Use javaw when you do not want the command prompt window to appear. However, the javaw launcher displays a dialog box with error information if for some reason the launch is not possible.

You declare:

In the above code, when return is used, it should return to the function that calls the main function.

There can be no other Java function (in fact, usually not) that calls the main() function. This is an agreement to declare a known entry point. If the JVM is started to run your main() class, then when main() returns, the JVM exits, except in a few special cases, for example. there are other threads not related to demons, or there is a stop hook.

+10
source share

Java Virtual Machine

+17
source share

Here is a good example of calling main() through JNI_CreateJavaVM .

+2
source share

See Starting Java programs ...

0
source share

The JVM uses main() as the starting point for the program, as int main() in C ++.

0
source share

Agree with the statements above, since the JVM calls the main method, since it is the entry point for any class that must be loaded to execute the class.

0
source share

Jvm starts the main thread to call the main method.

0
source share

All Articles