3

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?

1
  • 2
    Which IDE they are using is completely irrelevant to runtime behavior of the JVM, as is compiling a program from the command line. Commented Nov 6, 2017 at 14:33

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

A big difference between what you've added and what op posted, is the Application class is specified. So it isn't like java just finds a random Application and starts it.
@matt Very true. Edited to make it clear what is actually meant by "doesn't need a main(...) method".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.