Java: Check if command line arguments are null

后端 未结 6 1714
太阳男子
太阳男子 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 07:11

    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);
            }
    
        }
    
    

提交回复
热议问题