KingAnjrey KingAnjrey - 1 year ago 56
Java Question

JavaFX - Adding extra starting parameters

I worked with JavaFX because I had to programm an "interactive shell". I already programmed the Class Shell which connects to the remote host via SSH.

But the Shell has three parameters in his constructor:

public Shell(String username, String password, String host) {

this.username = username;
this.password = password; = host;

And what I would need (or would be perfect):

public void start(Stage primaryStage,String username, String password, String host) {
this.primaryStage = primaryStage; = new Shell(username,password,host);

Is there any way I could add extra/optional starting Parameter to the JavaFX start method. Or is there any other way how I could handle this problem?

Thank you in advance :)

Answer Source

You can access the command line parameters with Application.getParameters():

public void start(Stage primaryStage) {
    this.primaryStage = primaryStage;
    Application.Parameters parameters = getParameters();
    List<String> rawParams = parameters.getRaw();
    String userName = rawParams.get(0);
    String password = rawParams.get(1);
    String host = rawParams.get(2); = new Shell(username,password,host);

This code assumes there are (at least) three command line parameters. You probably want to add checks for the number of parameters and show an error message or prompt if they are missing, etc.