检查命令行上是否没有参数

Ama*_*rma 1 java

当我通过一些教程时,其中一个源代码文件有以下检查是否没有命令行参数:

if (null==args[0]) {
  System.err.println("Properties file not specified at command line");
  return;
}
Run Code Online (Sandbox Code Playgroud)

出于显而易见的原因,抛出ArrayIndexOutOfBoundsException并且不打印消息.

那么,如何检查并打印消息而不会抛出异常?

Ank*_*kit 7

if (args.length == 0) {
  System.err.println("Properties file not specified at command line");
  return;
}
Run Code Online (Sandbox Code Playgroud)

如果命令行中没有参数,则参数数组将为空.所以,你检查它的长度args.length==0.