谁在java中调用main函数? [英] Who calls the main function in java?

查看:252
本文介绍了谁在java中调用main函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在上面的代码中返回然后它应该返回调用main函数的函数。究竟是谁调用 main 函数?

In the above code when the return is used then it should return to the function which calls the main function. Who exactly calls the main function?

推荐答案

Java类在内执行更大的上下文(特别是其他人注意到的JVM)。以下是一些可能性:

Java classes are executed within a larger context (a particular JVM as others have noted). Below are some possibilities:

  • you run java -cp {classpath here} com.example.foo.SomeClass to explicitly select a class for the java application launcher to run
  • you run java -jar somejar.jar (the class in question will be selected in the .jar file's manifest)
  • you are working within Eclipse and use debug/run to execute a particular class's main() method.

在所有情况下, main()方法是在给定特定类的情况下执行代码的规范入口点。来自 java JVM上的文档:

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


描述

java工具启动Java应用程序。它通过启动Java运行时环境,加载指定的类以及调用该类的main方法来实现此目的。方法声明必须如下所示:

The java tool launches a Java application. It does this by starting a Java runtime environment, loading a specified class, and invoking that class's main method. The method declaration must look like the following:

   public static void main(String args[])

该方法必须声明为public和static,它不能返回任何值,并且必须接受String数组作为参数。默认情况下,第一个非选项参数是要调用的类的名称。应使用完全限定的类名。如果指定了-jar选项,则第一个非选项参数是包含应用程序的类和资源文件的JAR存档的名称,其中启动类由Main-Class清单标头指示。

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

Java运行时在三组位置中搜索启动类和其他使用的类:引导类路径,已安装的扩展和用户类路径。

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

将类名或JAR文件名传递给main函数后的非选项参数。

Non-option arguments after the class name or JAR file name are passed to the main function.

javaw命令与java相同,除了使用javaw,没有关联的控制台窗口。当您不希望出现命令提示符窗口时,请使用javaw。但是,如果由于某种原因启动失败,则javaw启动程序将显示一个包含错误信息的对话框。

The javaw command is identical to java, except that with javaw there is no associated console window. Use javaw when you don't want a command prompt window to appear. The javaw launcher will, however, display a dialog box with error information if a launch fails for some reason.

你说:


在上面的代码当使用return时它应该返回调用main函数的函数。

In the above code when the return is used then it should return to the function which calls the main function.

可能没有任何其他Java函数(实际上通常没有)调用 main()函数。这是宣布一个众所周知的切入点的惯例。如果启动JVM以运行类的 main()方法,那么当 main()返回时,JVM将退出,除了少数特殊情况,例如还有其他非守护程序线程在运行,或者有一个关闭钩子。

There may not be any other Java function (in fact there usually isn't) which calls the main() function. It's the convention for declaring a well-known entry point. If the JVM is launched to run your class's main() method, then when main() returns, the JVM exits, except in a few special cases, e.g. there are other non-daemon threads running or there is a shutdown hook.

这篇关于谁在java中调用main函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆