from my understanding every program needs a main method but I read a small article that stated JavaFx does not actually need a main method how is this remotely possible when the main method is essential to run a program?
Please show us the article.
As far as I can remember it is necessary to use a main method which calls the static launch method in the same class (extending Application); otherwise you have to specify the class with the launch method and pass a Class<T> object reference for the class containing the init and start methods, etc.
The JVM always looks for a public static void main(String args) method in the program to run. No matter if it's a simple console application, a JavaFX application or a web application.
However, that doesn't mean that you are always the one who writes the main method. Sometimes the framework itself provides the main method. For example, Java web applications normally run in a servlet container (such as Apache Tomcat). The main method is in the servlet container, not in your program itself.
Java applications are executed by a java launcher,
will run YourApp.class.
The java launcher is an executable which introspects the class passed as an argument and locates the main method, then starts program execution there.
The choice of the name "main" as the method to execute is historical (from C), and java just kept that by convention, it could have been called "foo" or "start" or something else, but it wasn't.
To be backward compatible with programs which have a main method, the java launcher will invoke the logic in the main method for a JavaFX Application if the class has a main method.
IMO, even though a JavaFX application doesn't need a main method, it is always best to include one. Some IDEs and tools such as debuggers (and programmers) have traditionally expected a main method and get confused or don't work when one is not there. So, it is best to have a main method which just invokes launch(args), that way the program will work the same in any environment, regardless of whether a main method is present or not.
The java command for Java 8+ gives you feedback on this:
Note that when you try to use the java launcher to launch a class which is not a JavaFX Application AND does not have a main method, then the launcher gives you an error message indicating what your two options are to make it work. Either, create a main method or have the class extend the JavaFX Application. But, as I said, if it is a JavaFX Application, then I recommend doing both.
Also, note that this is a bit different then something like a servlet running in a servlet container such as tomcat. With a servlet container, the container itself must have a main method to work. With a JavaFX application, there really doesn't need to be a main method anywhere.
I think the JEP related to launching JavaFX apps was: http://openjdk.java.net/jeps/153 and (I guess), it was under this JEP that the enhancement to launcher was performed so that it could work with a JavaFX Application that did not have a main method.
Honk if you love justice! And honk twice for tiny ads!
Free, earth friendly heat - from the CodeRanch trailboss