运行 没有 main 方法的 JavaFX 应用

Running JavaFX app without main method

This material 在第 10 页上说可以 运行 JavaFX 应用程序无需编写 main。我想 jfxrt.jar 里面有一些预定义的 main 寻找 class 扩展 Application 和 运行 。

是吗?怎么做?

I suppose there is some predefined main inside jfxrt.jar which looks for a class extending Application and runs it.

这不是该评论的真正含义,也不是它的工作原理。它只是说 "main class" 不需要定义 main(String[] args) 方法,如果它是 javafx.application.Application 的子 class。来自 Oracle tools documentation for java:

The java command can be used to launch a JavaFX application by loading a class that either has a main() method or that extends the javafx.application.Application. In the latter case, the launcher constructs an instance of the Application class, calls its init() method, and then calls the start(javafx.stage.Stage) method.

(我的重点)

因此,如果在命令行上指定的 class 是 Application 的子 class,则此行为会简单地嵌入到 JVM 可执行文件中。请注意,您仍然必须指定要成为 运行 的 class;如果它是 Application subclass,则不需要 main 方法。 (JVM 没有扫描候选 class 到 运行 的 class 路径,正如您在问题中所描述的那样。)

将 class 变为 运行 可以在命令行 (java com.mycompany.MyApp) 中指定,也可以按照通常的方式在 jar 文件清单中指定。

这是在 JDK 8, iirc 中添加的。