Solved

parsing command line options in Java

Posted on 2011-09-09
14
189 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
Technology Partners: 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

735 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