Solved

parsing command line options in Java

Posted on 2011-09-09
14
182 Views
Last Modified: 2012-05-12
i want to write an application

java -jar TEST.jar -u username -h host   etc.....

can someone point me to example of parsing this.
0
Comment
Question by:learningunix
  • 6
  • 5
  • 3
14 Comments
 
LVL 15

Expert Comment

by:Minh Võ Công
ID: 36509297
0
 

Author Comment

by:learningunix
ID: 36509376
I don't have

import org.apache.commons.cli.*;

Is ther any way without inastalling new java packages?
0
 
LVL 15

Expert Comment

by:Minh Võ Công
ID: 36509435
public class Echo {
    public static void main (String[] args) {
        for (String s: args) {
            System.out.println(s);
        }
    }
}
http://download.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:learningunix
ID: 36509452
I am looking something similar to waht we have in C++ GetOpt
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36509482
If you organise the args well (as you have) then you should be able to use a Map
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 36509499
You can then look up the argument by name e.g.
import java.util.*;
import java.io.*;

public class ArgParser {
    public static void main(String[] args) {
	System.out.println(ArgParser.getOpts(args));
    }

    private static Map<String, String> getOpts(String[] args) {
	Map<String, String> result = new HashMap<String, String>(args.length);
	for(int i = 0;i < args.length;i+=2) {	
	    result.put(args[i], args[i+1]); // trim the leading '-' character if you want from args[i]
	}
	return result;
    }
}

Open in new window

0
 
LVL 15

Assisted Solution

by:Minh Võ Công
Minh Võ Công earned 125 total points
ID: 36509500
0
 

Author Comment

by:learningunix
ID: 36510464
I just downloaded jot-simple.
I'll give it a try and see

so when I create my own jar file do I have to give all class file of jotsimple also ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36510527
>>
I just downloaded jot-simple.
I'll give it a try and see
>>

I thought you didn't want to install extra software..?
0
 

Author Comment

by:learningunix
ID: 36510596
I'll try both and see.

Today is my first day of java programming and trying to do too many things at once and probably jumping a lot.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36510802
>>Today is my first day of java programming

In that case keep it simple: use what i posted and you can go for a more reusable and extensible solution later
0
 

Author Comment

by:learningunix
ID: 36510876
I found one more tool
jargs.gnu which is also good one.
0
 

Author Closing Comment

by:learningunix
ID: 36510892
Thanks, I am planning to use jargs.gnu as it is fairly simple.

Thanks once again for the direction.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36511619
:)
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
eclipse java  build path 6 57
split string containing \r\n in Java 46 44
expectj telnet failing 5 36
Is Applet the way to go for my drag and drop system? 8 14
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question