且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

不使用main方法运行JavaFX应用程序

更新时间:2021-09-28 01:17:43


我想有一些预定义的 main jfxrt.jar 里面查找一个扩展 Application 的类并运行它。

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

这不是真正意义上的评论,而是它的工作原理。它只是说主类不需要定义 main(String [] args)方法,如果它是 javafx.application.Application 。来自 java $的Oracle工具文档c $ c>

This isn't really what's meant by that comment, and isn't how it works. All it is saying is that the "main class" doesn't need to define a main(String[] args) method if it is a subclass of javafx.application.Application. From the Oracle tools documentation for java:


可以使用 java 命令通过加载具有 main()方法或扩展 javafx.application.Application 。在后一种情况下,启动器构造 Application 类的实例,调用其 init()方法,然后调用 start(javafx.stage.Stage)方法

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.

(我的重点)

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

So if the class specified on the command line is a subclass of Application, this behavior is simply baked into the JVM executable. Note that you still have to specify the class that is to be run; it just doesn't need a main method if it is an Application subclass. (The JVM is not scanning the classpath for candidate classes to run, as you seem to be describing in the question.)

可以在命令行( java com.mycompany.MyApp )或者可以通常的方式在jar文件清单中指定。

The class to be run can be specified on the command line (java com.mycompany.MyApp) or can be specified in a jar file manifest in the usual way.

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

This was added in JDK 8, iirc.