Solved

parsing command line options in Java

Posted on 2011-09-09
14
191 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

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 first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

739 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