Java - Get Command Line Parameters

15/07/18 Permalink

I couldn't find a simple way of parsing command line parameters passed to programs without needlessly importing libraries. String[]args is parsed using spaces only, so if you want more advanced parameters were you name the values and have multiple values like java Prog -opt0 blah blah2 -opt1 blah you'll need to parse it yourself.

Here's a simple way of doing more advanced command line parsing in Java:

 public HashMap<String,ArrayList<String>> getCommandLineOptions(String[]args){
   	HashMap<String,ArrayList<String>> opts = null;
        String opt = null;	
	for(String arg:args){
	   	}else if(opt!=null){
		      ArrayList oopts = opts.get(opt);
			  oopts=new ArrayList();
	return opts;  			
Share It: Digg | | Furl | reddit | Facebook | Yahoo! | Send to Phone | Feedback