• Post Reply Bookmark Topic Watch Topic
  • New Topic

JavaFx doesn't need a main method? mind blown  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 517
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Adam Chalkley
Ranch Hand
Posts: 517
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and in another thread I read I seen many programmers saying that a main method is required no matter what,which one to believe?
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesse Matty
Ranch Hand
Posts: 71
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought all java programs needed a main method but , in all my javafx code I write I always write  to start the program with a GUI Stage

 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

javac -cp "C:\Program Files\Java\jre1.8.0_45\lib\ext\jfxrt.jar" Test.java
java Test


 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16048
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Damien Smith
Ranch Hand
Posts: 352
21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A JavaFX application does not need a main method.

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.

The java launcher was modified for JavaFX programs so that it could run programs which inherit from the JavaFX Application class but do not include a main method, it does this by launching the application and invoking init, start and stop on the application as per the JavaFX application lifecycle (https://docs.oracle.com/javase/8/javafx/api/javafx/application/Application.html).
Details of how this works are here:
https://stackoverflow.com/questions/29424612/how-would-one-create-a-custom-jvm-launcher

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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!