How to handle empty parameters in a main method java call -
i have dynamic way of passing in parameters java main method call done via command line(cmd) runnable jar file. @ moment main() method takes 6 parameters , sets each 1 variable before calling method variables passed in.
what id way give user ability pass 5 or less paramters command line , have safely handle missed parameter setting null or empty string("") value.
for example, if ran command below, should know set missing parameters have not specified(clientname , outputfolder), empty string.
java -xmx1024m -jar mainapp.jar "summary" **<missing>** "2015-06-07" "https://12345.bp.com/bp/" "c:\\parameters.txt" **<missing>**
here code have 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 appreciated.
suppose example last 2 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]);
Comments
Post a Comment