不使用main方法运行JavaFX应用程序 [英] Running JavaFX app without main method

查看:1295
本文介绍了不使用main方法运行JavaFX应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此资料在第10页上说明可以在不编写 main 的情况下运行JavaFX应用程序。我想在 jfxrt.jar 中有一些预定义的 main ,它会查找扩展应用程序的类并运行它。

This material says on page 10 that it is possible to run a JavaFX app without writing main. I suppose there is some predefined main inside jfxrt.jar which looks for a class extending Application and runs it.

是这样吗?怎么做?

推荐答案


我想有一些预定义的 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

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 <的类来启动JavaFX应用程序code>。在后一种情况下,启动器构造 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.

这篇关于不使用main方法运行JavaFX应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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