Java:检查命令行参数是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3868878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 06:06:12  来源:igfitidea点击:

Java: Check if command line arguments are null

javacommand-linecommand-line-arguments

提问by Bobby S

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.

但是,这会返回一个数组越界异常,这是有道理的。我只是在寻找正确的用法。

采纳答案by jjnguy

The arguments can never be null. They just wont exist.

参数永远不可能是null。他们只是不会存在。

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

回答by Puspendu Banerjee

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:

如果你不传递任何参数,那么即使在这种情况下 args 也会被初始化但没有任何项目/元素。尝试以下一种,您将获得相同的效果

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

    }

回答by Stephen C

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

@jjnguy 的答案在大多数情况下都是正确的。如果通过运行应用程序以正常方式从命令行运行,您将永远不会null在参数数组(或null数组)中看到字符串main

However, if some other part of the application calls a mainmethod, it is conceivable that it might pass a nullargument or nullargument array.

但是,如果应用程序的其他部分调用main方法,则可以想象它可能会传递null参数或null参数数组。

However(2), this is clearly a highly unusual use-case, andit is an egregious violation of the implied contract for a mainentry-point method. Therefore, I don't think you should bother checking for nullargument 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.

但是(2),这显然是一个极不寻常的用例,并且它是一个严重违反了隐含合同的main入口点的方法。因此,我不认为你应该费心检查null的参数值main。在不太可能发生的情况下,调用代码获取NullPointerException. 毕竟,违反合同是调用方的错误。

回答by fastcodejava

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

您应该检查(args == null || args.length == 0). 虽然null不是真的需要检查,但这是一个很好的做法。

回答by Sean Crouse

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.exeas 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 NULLto the main method, in which case args will be null.

args 变量本身可能为空,但不是通过正常执行。正常执行将java.exe用作命令行的入口点。但是,我看到一些程序使用C++带有 JNI 的编译代码来使用jvm.dll,完全绕过 java.exe。在这种情况下,可以传递NULL给 main 方法,在这种情况下 args 将为空。

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

我建议始终检查if ((args == null) || (args.length == 0)),或if ((args != null) && (args.length > 0))根据您的需要。