user3754136
user3754136

Reputation: 529

how to pass string arguments in java as argument at runtime

I am new to Java I need help with as how can I take values at runtime when a Java file is executed. I have Java program where the values of host,user, password and command is hardcoded, how can I parmeterize it. eg,

    public static void main(String[] args) {
        String host="ssh.journaldev.com";
        String user="sshuser";
        String password="sshpwd";
        String command1="ls -ltr";
        try{....

Can we make any changes such that at runtime on cmdline i can pass all values. eg.
java -jar testjava ssh.journaldev.com sshuser sshpwd "ls -ltr" this should be executed.

Upvotes: 2

Views: 3272

Answers (1)

Dermot Blair
Dermot Blair

Reputation: 1620

Yes this is done like the following:

String name = args[0];
String user = args[1];
String password=args[2];
String command1=args[3];

Upvotes: 2

Related Questions