Reputation: 529
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
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