Java: Check if command line arguments are null

后端 未结 6 1712
太阳男子
太阳男子 2020-11-28 06:34

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

public static void main(String[] args)
{
    if(args[0] == null)
    {
        System.ou         


        
6条回答
  •  失恋的感觉
    2020-11-28 06:54

    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.

提交回复
热议问题