Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8920747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:21:21+00:00 2026-06-15T06:21:21+00:00

This is first time am using apache commons-cli. Requirement : i want to use

  • 0

This is first time am using apache commons-cli.

Requirement :
i want to use CommandLine and Options in commons-cli to pass my runtime parameters to my java class.

Scenario :
main class – com.test.mian.MyClass

i can run my class from command line by

java -cp $classPath  com.test.mian.MyClass -one 1 -two 2 -three 3

how can i do the same from a method of another class by passing these arguments using CommandLine and Options of commons-cli.

and also if there is any other way other than

System.setProperty("key","value");

please suggest too.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:21:22+00:00Added an answer on June 15, 2026 at 6:21 am

    Here’s a very stripped down version of something I use. It basically sets up a singleton that gets initialized once and then can be used wherever you want within your program. I chose to store the info in HashMap’s and ArrayList’s because they were easier to deal with later.

    //****************************************************************************
    //***** File Name: MianCLIOptions.java
    //****************************************************************************
    
    package com.test.mian;
    
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    import org.apache.commons.cli.AlreadySelectedException;
    import org.apache.commons.cli.CommandLine;
    import org.apache.commons.cli.CommandLineParser;
    import org.apache.commons.cli.GnuParser;
    import org.apache.commons.cli.MissingArgumentException;
    import org.apache.commons.cli.MissingOptionException;
    import org.apache.commons.cli.Option;
    import org.apache.commons.cli.Options;
    import org.apache.commons.cli.ParseException;
    import org.apache.commons.cli.UnrecognizedOptionException;
    import org.apache.commons.lang.WordUtils;
    
    
    //****************************************************************************
    //****************************************************************************
    //****************************************************************************
    //****************************************************************************
    public class MianCLIOptions
    {
        //***** constants *****
    
        //***** public data members *****
    
        //***** private data members *****
        private static MianCLIOptions singletonObj = null;
    
        private HashMap<String,Object> options = new HashMap<String,Object>();
        private ArrayList<String> arguments = new ArrayList<String>();
    
        //****************************************************************************
        public static MianCLIOptions getopts()
        {
            if (singletonObj == null) {
                throw new IllegalStateException("[MianCLIOptions] Command line not yet initialized.");
            }
    
            return singletonObj;
        }
    
        //****************************************************************************
        public static synchronized void initialize(Options optsdef, String[] args)
            throws MianCLIOptionsException, UnrecognizedOptionException, MissingArgumentException,
                   MissingOptionException, AlreadySelectedException, ParseException
        {
            if (singletonObj == null) {
                singletonObj = new MianCLIOptions(optsdef, args);
            }
            else {
                throw new IllegalStateException("[MianCLIOptions] Command line already initialized.");
            }
        }
    
        //****************************************************************************
        //----- prevent cloning -----
        public Object clone() throws CloneNotSupportedException
        {
            throw new CloneNotSupportedException();
        }
    
        //****************************************************************************
        public boolean isset(String opt)
        {
            return options.containsKey(opt);
        }
    
        //****************************************************************************
        public Object getopt(String opt)
        {
            Object rc = null;
    
            if (options.containsKey(opt)) {
                rc = options.get(opt);
            }
    
            return rc;
        }
    
        //****************************************************************************
        //***** finally parse the command line
        //****************************************************************************
        private MianCLIOptions(Options optsdef, String[] args)
            throws UnrecognizedOptionException, MissingArgumentException,
                   MissingOptionException, AlreadySelectedException, ParseException
        {
            //***** (blindly) parse the command line *****
            CommandLineParser parser = new GnuParser();
            CommandLine cmdline = parser.parse(optsdef, args);
    
            //***** store options and arguments *****
            //----- options -----
            for (Option opt : cmdline.getOptions()) {
                String key = opt.getOpt();
                if (opt.hasArgs()) {
                    options.put(key, opt.getValuesList());
                }
                else {
                    options.put(key, opt.getValue());
                }
            }
    
            //----- arguments -----
            for (String str : cmdline.getArgs()) {
                //----- account for ant/build.xml/generic -----
                if (str.length() > 0) {
                    arguments.add(str);
                }
            }
        }
    }
    
    //****************************************************************************
    //***** EOF  ***** EOF  ***** EOF  ***** EOF  ***** EOF  ***** EOF  **********
    

    In your main, you can then call it like so:

        //***** build up options *****
        Options options = new Options();
    
        // ... .... ...
    
        //***** process command line *****
        try {
            MianCLIOptions.initialize(options, args);
        }
        catch (UnrecognizedOptionException ex) {
            // do something
        }
    

    And finally, in some other class you can call it like so:

        MianCLIOptions opts = MianCLIOptions.getopts();
        if (opts.isset("someopt")) {
            // do something exciting
        }
    

    Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Apache Commons for the first time, mainly because I wanted access to
First, this is my first time with Apache Derby. I am using netbeans, willing
This is my first time using XML documents. What I'm trying to do is
This is my first time using this site and I am quite new to
This is my first time using lightbox which uses jquery framework. But when I
This is my first time using an external library, and I'm a bit nervous
This is my first time using jQuery and I am pleased to get my
This is my first time using Google Analytics Ecommerce Tracking to get data for
this is my first time using StAX for parsing XML documents (still in the
So this is my first time using cookies and i'm having some trouble setting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.