java 参数处理,如何在主要方法Java调用中处理空参数

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.

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.

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" **** "2015-06-07" "https://12345.bp.com/bp/" "c:\\Parameters.txt" ****

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

}

}

Any help appreciated.

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值