JavaFX没有调用main(String [] args)方法

时间:2015-11-05 16:28:43

标签: java intellij-idea javafx

我开发了可以GUI执行的程序,CLI取决于提供的参数。这是应用程序类:

public class Main extends Application {

    public static void main(String[] args) {
        if (args != null && args.length > 0 && args[0].equals("cli")) {
            String pathToProperties = args[1];
            Cli cli = new Cli(pathToProperties);
            cli.loadPropertiesAndGenerateApk();
        } else {
            launch(args);
        }
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("view/sample.fxml"));

        Parent root = (Parent) loader.load();
        primaryStage.setTitle("Allowed Site Configurator");
        primaryStage.setScene(new Scene(root, 800, 800));
        primaryStage.show();
    }
}

当我构建工件时,Intellij IDEA创建MANIFEST.MF

Manifest-Version: 1.0
JavaFX-Version: 2.2
JavaFX-Application-Class: sample.Main
Created-By: JavaFX Packager
Main-Class: com/javafx/main/Main

当我调用java -jar JavaFXApp.jar "cli" ~/config.properties程序时,只调用start()方法而不先调用main()方法。

问题是:如何强制首先调用main()方法?

1 个答案:

答案 0 :(得分:3)

为GUI创建一个单独的类:

public class Main {

    public static void main(String[] args) {
        if (args != null && args.length > 0 && args[0].equals("cli")) {
            String pathToProperties = args[1];
            Cli cli = new Cli(pathToProperties);
            cli.loadPropertiesAndGenerateApk();
        } else {
            Application.launch(GUIApp.class, args);
        }
    }
}

public class GUIApp extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("view/sample.fxml"));

        Parent root = (Parent) loader.load();
        primaryStage.setTitle("Allowed Site Configurator");
        primaryStage.setScene(new Scene(root, 800, 800));
        primaryStage.show();
    }

}

并配置构建,以便Main是主类。