命令提示符中的javafx参数给出空值

命令提示符中的javafx参数给出空值

本文介绍了命令提示符中的javafx参数给出空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与大家晚安

这可能是一个愚蠢的问题,但我似乎很难找到答案.我创建了一个简单的JavaFX8程序,该程序应该能够读取命令行参数.

This might be a silly question but I seem to struggle finding an answer to it. I've created a simple JavaFX8 program that should be able to read command line arguments.

让我举例说明:

public void start(Stage stage) throws Exception {
    Map parameters = getParameters().getNamed();

    System.out.println("parameter is " + parameters.get("myKey"));

    ...
}

当我在NetBeans中定义值为 abc 的名为 myKey 的参数时,

When I define a parameter named myKey in NetBeans with value abc,

当我从IDE运行我的应用程序时,它会产生以下输出:

it results in the following output when I run my application from the IDE:

parameter is abc

但是,如果我在命令提示符下运行它,如下所示:

However, if I run it from the command prompt as following:

java -jar MyApp.jar myKey=abc

它返回值 null ,这意味着参数未转发到JavaFX应用程序:

it returns value null, which means the parameters isn't forwarded to the JavaFX application:

parameter is null

这是为什么?如果答案真的很简单,这是我第一次使用参数等道歉.

Why is this? It's the first time I'm working with parameters so apologies if the answer is really easy.

推荐答案

从命令行调用时,关键是使用以下语法:

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

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

getNamed仅在参数用-注释时返回什么(我认为这等于'NAMED')

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);
    }
}

这将打印:

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

这篇关于命令提示符中的javafx参数给出空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 02:38