0

I am attempting to pass arguments into a main method at execution time. The idea is eventually I'll be integrating this into code that can send an email/text notification, which another program will use to send emails automatically.

at runtime in cmd, I am in the correct folder and the filename is PassArgs.jar.

I typed "java -jar PassArgs.jar em" (without quotes) expecting to get the output '1', but instead the console displayed "Invalid argument". I added the statement in that block to print out the first element in the array vars (which is a string array) and sure enough, em was returned.

What am I doing wrong? Why isn't Java recognizing 'em' as a valid argument? Any help would be appreciated. Thanks!

public class PassArgs {

public static void main(String[] vars) {



    //check to see if too many arguments are passed
    if (vars.length!=1)
    {
        System.out.println("Not correct number of args");
    }

    else
    {
    if (vars[0] == "em")
    {
        System.out.println("1");
        //Read email addresses from file
        //Send a physical email to a @domain.com address
    }
    else if (vars[0] == "te")
    {
        System.out.println("2");
        //read phonenumber@domain.com from file
        //Send a text message to mobile phones. 
    }
    else if (vars[0] == "bo")
    {
        System.out.println("3");
        //read from both documents
        //Send email and text to mobile phones
    }
    else
    {
        System.out.println("Invalid argument");
        System.out.println(vars[0]);
    }
    }

}

}
Andy Chao
  • 31
  • 5
  • 2
    [`Use equals to compare strings`](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – sam Nov 06 '15 at 22:28
  • try `equals()` when comparing `Strings` - see http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – blurfus Nov 06 '15 at 22:29
  • I never replied to this comment, but thanks! helped a lot :) – Andy Chao Dec 30 '15 at 03:13

0 Answers0