Java parsing command-line arguments (COMMON-CLI) Exercise

Source: Internet
Author: User

 Packagefoo;ImportOrg.apache.commons.cli.BasicParser;ImportOrg.apache.commons.cli.CommandLine;ImportOrg.apache.commons.cli.CommandLineParser;Importorg.apache.commons.cli.Options; Public classTest { Public Static voidMain (string[] args)throwsexception{commandlineparser Parser=NewBasicparser (); Options Options=NewOptions (); Options.addoption ("H", "Help",false, "Print This usage information"); Options.addoption ("V", "verbose",false, "Print Out VERBOSE Information"); Options.addoption ("F", "file",true, "File to save program output to"); //Parse The program argumentsCommandLine CommandLine =parser.parse (options, args); //Set the appropriate variables based on supplied options        BooleanVERBOSE =false; String file= ""; if(Commandline.hasoption (' H ') {System.out.println ("Help Message"); System.exit (0); }                if(Commandline.hasoption (' V ') ) {verbose=true; }                if(Commandline.hasoption (' F ') ) {file= Commandline.getoptionvalue (' F '); }    }}

A class that implements the Commandlineparser interface can parse a String array according to the Options specified and re Turn a CommandLine.

// Get configuration file String configxmlfile = System.getproperty ("Config"); // Load configuration file New Filesystemxmlapplicationcontext (                configxmlfile); // Mbeanserver is a repository that contains all the registered Mbean. It is the core of the JMX agent layer Mbeanserver mbs = java.lang.management.ManagementFactory                . Getplatformmbeanserver ();

 logger.info ("register ApplicationServer.")        );        ObjectName mbeanname  = Getapplicationobjectname (); ApplicationServer applicationserver  = new          ApplicationServer ();        Applicationserver.setapplicationcontext (ApplicationContext); Mbs.registermbean (ApplicationServer, mbeanname);  public  static   ObjectName Getapplicationobjectname ()  throws   Malformedobjectnameexception {ObjectName Mbeanname  = new  ObjectName ("Fm5:name=applicationserver" );     return   Mbeanname; }

Java parsing command-line arguments (COMMON-CLI) Exercise

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.