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.
Is that so? How to do that?
I suppose there is some predefined
main
insidejfxrt.jar
which looks for a class extendingApplication
and runs it.
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
:
The
java
command can be used to launch a JavaFX application by loading a class that either has amain()
method or that extends thejavafx.application.Application
. In the latter case, the launcher constructs an instance of theApplication
class, calls itsinit()
method, and then calls thestart(javafx.stage.Stage)
method.
(My emphasis)
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.)
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.
This was added in JDK 8, iirc.