如何在主要方法Java调用中处理空参数 [英] How to handle empty parameters in a main method java call

查看:185
本文介绍了如何在主要方法Java调用中处理空参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一种动态的方法,可以通过命令行(cmd)将参数传递给java main方法调用,该方法通过Runnable JAR文件完成.目前,我的main()方法有6个参数,并将每个参数设置为一个变量,然后再调用另一个传入这些变量的方法.

I would like to have a dynamic way of passing in parameters to a java main method call which is done via the Command Line(cmd) to a Runnable JAR file. At the moment my main() method takes 6 parameters and sets each one to a variable before calling another method with those variables passed in.

什么样的id是一种使用户能够将5个或更少的参数传递给命令行并通过将其设置为null或空string(")值来安全处理错过的参数的方法.

What id like is a way to give a user the ability to pass 5 or less paramters to the command line and have it safely handle the missed parameter by setting it to null or an empty string("") value.

例如,如果我在下面运行了命令,它应该知道将未指定的缺失参数(clientName和outputFolder)设置为空String.

For example, if I ran the command below, it should know to set the missing parameters I have not specified(clientName and outputFolder), to an empty String.

java -Xmx1024m -jar MainApp.jar "Summary" **<missing>** "2015-06-07" "https://12345.bp.com/bp/" "c:\\Parameters.txt" **<missing>**

这是我主要方法的代码:

Here is the code I have for my main method:

public static void main(String[] args) {
        try {               
            String dType = args[0];
            String clientName = args[1];
            String cycleString = args[2];
            String mspsURL = args[3];
            String inputFile = args[4];
            String outputFolder = args[5];

            System.out.println("**Main Parameters passed**");
            for(String x : args) {
                System.out.println(x);
            }

            runLogic(dType, clientName, cycleString, 
                    mspsURL, inputFile, outputFolder);          
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }

任何帮助表示赞赏.

推荐答案

例如,假设最后两个是可选的.

Suppose for example that last two are optional.

String dType = args[0];
String clientName = args[1];
String cycleString = args[2];
String mspsURL = args[3];
String inputFile = (args.length < 4 ? "default inputFile" : args[4]);
String outputFolder = (args.length < 5 ? "default outputFolder" : args[5]);

这篇关于如何在主要方法Java调用中处理空参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆