JavaFX没有调用main(String [] args)方法 [英] JavaFX not calling main(String[] args) method

查看:135
本文介绍了JavaFX没有调用main(String [] args)方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've developed program that can be executed as GUI and CLI depends on parameters are provided. Here is application class:

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

And when I build artifacts Intellij IDEA create 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.jarcli〜/ config.properties program仅调用 start()方法而不先调用 main()方法。

When I call java -jar JavaFXApp.jar "cli" ~/config.properties program invoke only start() method without calling main() method first.

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

Question is: How to force to call main() method first?

推荐答案

为GUI创建一个单独的类:

Create a separate class for the 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 是主类。

and configure the build so that Main is the main class.

这篇关于JavaFX没有调用main(String [] args)方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆