javafx parameters in command prompt gives null values

会有一股神秘感。 提交于 2019-12-06 09:23:13

The key is to use the following syntax when calling from command-line:

java -jar JavaHelp.jar --p1=hello --p2=world

getNamed only returns something if parameter is annotated with -- (I think this equals 'NAMED')

Try it with this program and you can see:

public class Main extends Application {

    @Override
    public void init() throws Exception {
        super.init();
        System.out.println(getParameters().getRaw().toString());
        getParameters().getNamed().forEach((name, string) -> {
            System.out.println("Parameter[" + name + "]=" + string);
        });
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(new Pane() {{
            getChildren().add(new Button("B"));
        }}));
        primaryStage.show();

    }


    public static void main(String[] args) {
        launch(args);
    }
}

This will print:

Parameter[p1]=hello
Parameter[p2]=world

You need -- before each argument. So the command you need is:

java -jar MyApp.jar --myKey=abc
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!