JDK-8177294 : add command line switch for JavaFX Application vs main()
  • Type: Enhancement
  • Component: javafx
  • Sub-Component: base
  • Affected Version: 9
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_7
  • CPU: x86_64
  • Submitted: 2017-03-20
  • Updated: 2017-03-21
  • Resolved: 2017-03-21
Related Reports
Relates :  
Description
A DESCRIPTION OF THE REQUEST :
Currently, if a class file has both a main() method and is a JavaFX Application, launching with "java <classname>" or launching similarly in jdb will always run the main() method. It would be nice if there were a command line switch to force a JavaFX-style launch in that case.


JUSTIFICATION :
For a JavaFX application, the main() method may be a test, or may be designed to run the program in a different way from a normal JavaFX launch.



EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Launch the JavaFX application at the command line by running:

java -javafx HelloWorld2

or similar.
ACTUAL -
There is no easy way to do that when a main() method is present.


---------- BEGIN SOURCE ----------
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
 
public class HelloWorld2 extends Application {

   public static int xx = 3;
    
   Label label = new Label();

   @Override
    public void start(Stage primaryStage) {
      label.setText("Hello World");
      StackPane root = new StackPane();
      root.getChildren().add(label);
   
      Scene scene = new Scene(root, 300, 250);
   
      primaryStage.setTitle("Hello World!");
      primaryStage.setScene(scene);
      primaryStage.show();
   }
   
   
   public static void main(String[] args) {
      System.out.println("Test");
   }
}

---------- END SOURCE ----------


Comments
Calling the main method is intentional and was done in JDK 8 to fix JDK-8102035. An application can always put its main method in a different class than the one that extends javafx.application.Application and have the same flexibility.
21-03-2017

If the class has extended Application and start method is overridden (valid javafx application) along with that it has main method, which can be run as stand alone. By default main method is called, Enhancement is to provide runtime argument to control on the launcher to differenciate between java and javafx type. something like this "java -javafx HelloWorld2 "
21-03-2017