Hao Wu
Hao Wu

Reputation: 143

Run java in terminal with and without arguments at same time

I have pw_check.java and I need to run it with an argument first and then run it without argument in terminal.

java pw_check -g

java pw_check

But in second command, without argument, the system is throwing exception. How could I handle it to feed my requirement.

Upvotes: 0

Views: 202

Answers (1)

Davide Lorenzo MARINO
Davide Lorenzo MARINO

Reputation: 26926

Check the code pw_check.java.

Probably there is something like

public static void main(String[] args) {

    // Code accessing args[0]

}

This will cause an error if you don't have a parameter.

Modify with a code similar to the following:

public static void main(String[] args) {
    String arg = DEFAULT_ARG;
    if (args.length == 1) {
        arg = args[0];
    }
    ... // Code using arg DEFAULT or passed value
}

Upvotes: 1

Related Questions