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