99

I am looking to do some error checking for my command line arguments

public static void main(String[] args)
{
    if(args[0] == null)
    {
        System.out.println("Proper Usage is: java program filename");
        System.exit(0);
    }
}

However, this returns an array out of bounds exception, which makes sense. I am just looking for the proper usage.

jjnguy
  • 136,852
  • 53
  • 295
  • 323
Bobby S
  • 4,006
  • 9
  • 42
  • 61

6 Answers6

177

The arguments can never be null. They just won't exist.

In other words, what you need to do is check the length of your arguments.

public static void main(String[] args) {
  // Check how many arguments were passed in
  if (args.length == 0) {
    System.out.println("Proper Usage is: java program filename");
    System.exit(0);
  }
}
Jeff Schaller
  • 2,352
  • 5
  • 23
  • 38
jjnguy
  • 136,852
  • 53
  • 295
  • 323
20

@jjnguy's answer is correct in most circumstances. You won't ever see a null String in the argument array (or a null array) if main is called by running the application is run from the command line in the normal way.

However, if some other part of the application calls a main method, it is conceivable that it might pass a null argument or null argument array.

However(2), this is clearly a highly unusual use-case, and it is an egregious violation of the implied contract for a main entry-point method. Therefore, I don't think you should bother checking for null argument values in main. In the unlikely event that they do occur, it is acceptable for the calling code to get a NullPointerException. After all, it is a bug in the caller to violate the contract.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
12

To expand upon this point:

It is possible that the args variable itself will be null, but not via normal execution. Normal execution will use java.exe as the entry point from the command line. However, I have seen some programs that use compiled C++ code with JNI to use the jvm.dll, bypassing the java.exe entirely. In this case, it is possible to pass NULL to the main method, in which case args will be null.

I recommend always checking if ((args == null) || (args.length == 0)), or if ((args != null) && (args.length > 0)) depending on your need.

amesh
  • 1,311
  • 3
  • 21
  • 51
Sean Crouse
  • 129
  • 1
  • 2
1

You should check for (args == null || args.length == 0). Although the null check isn't really needed, it is a good practice.

fastcodejava
  • 39,895
  • 28
  • 133
  • 186
  • 2
    Unnecessary checks are NOT good practice. The `null` check is unnecessary, but you are correct that the `length` check is necessary, – Stephen C Jan 17 '19 at 00:59
0

if i want to check if any speicfic position of command line arguement is passed or not then how to check? like for example in some scenarios 2 command line args will be passed and in some only one will be passed then how do it check wheather the specfic commnad line is passed or not?

public class check {

public static void main(String[] args) {
if(args[0].length()!=0)
{
System.out.println("entered first if");
}
if(args[0].length()!=0 && args[1].length()!=0)
{
System.out.println("entered second if");
}
}
}

So in the above code if args[1] is not passed then i get java.lang.ArrayIndexOutOfBoundsException:

so how do i tackle this where i can check if second arguement is passed or not and if passed then enter it. need assistance asap.

  • 1
    Please make this a question. Since it does not answer the question, it doesn't belong here. By asking a question instead, you have a much better chance of getting an answer. – AlexH Oct 20 '20 at 19:10
-10

If you don't pass any argument then even in that case args gets initialized but without any item/element. Try the following one, you will get the same effect:

 
public static void main(String[] args) throws InterruptedException {
        String [] dummy= new String [] {};
        if(dummy[0] == null)
        {
            System.out.println("Proper Usage is: java program filename");
            System.exit(0);
        }

    }

  • 1
    This is totally incorrect. If an application is launched with no command line arguments, the `main` method will be called with an `args` array whose length is zero. – Stephen C Oct 06 '10 at 03:17
  • @Stephen An array without any element inside, has zero length. Then what's wrong about my answer? – Puspendu Banerjee Oct 27 '10 at 21:15
  • @Puspendu - what is wrong is that `dummy[0]` throws an array index out of bounds exception!! Ditto if you change the program (back) to testing `args[0]` and run it with no arguments. – Stephen C Oct 27 '10 at 21:35
  • @Stephen I have not given any solution rather I tried to describe the issue. Read "If you don't pass any argument then even in that case args gets initialized but without any item/element. Try the following one, you will get the same effect:" – Puspendu Banerjee Oct 28 '10 at 03:32
  • 1
    OK - I understand now. It would have helped if you hadn't mistyped your first sentence. As written it is unintelligible. I note that you quietly corrected it in your last comment. Sneaky. – Stephen C Oct 28 '10 at 04:04
  • Well, It's nothing "Sneaky", I have just corrected a TYPO "the" to "then" – Puspendu Banerjee Oct 28 '10 at 04:13
  • ... without saying so, and at the same time implying that I hadn't read your answer properly. That's the sneaky part! – Stephen C Oct 28 '10 at 05:16
  • Please check now, I have corrected it on the main post.I appreciate your criticism will help me to become a better writer in future! – Puspendu Banerjee Oct 28 '10 at 10:43
  • 1
    "I have not given any solution rather I tried to describe the issue". Then don't post an answer. Post a comment. – Stealth Rabbi Oct 20 '15 at 14:13
  • The "corrected" version doesn't actually illustrate anything. You are just trying to test a "dummy" variable, not the actual parameters. And what is more, if you run this "corrected" code you will get an uncaught exception ... not your usage message. The best thing you can do with this Answer is delete it. – Stephen C Mar 31 '18 at 08:27