Goodevening everyone,
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.
Let me illustrate:
public void start(Stage stage) throws Exception {
Map parameters = getParameters().getNamed();
System.out.println("parameter is " + parameters.get("myKey"));
...
}
When I define a parameter named myKey in NetBeans with value abc,
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
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 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
来源:https://stackoverflow.com/questions/31572862/javafx-parameters-in-command-prompt-gives-null-values
