我开发了可以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()
方法?
答案 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
是主类。