Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running JavaFX app without main method

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?

like image 904
André Willik Valenti Avatar asked Sep 09 '26 11:09

André Willik Valenti


1 Answers

I suppose there is some predefined main inside jfxrt.jar which looks for a class extending Application 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 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.

(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.

like image 83
James_D Avatar answered Sep 10 '26 23:09

James_D



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!