title: Create a command line interface to a Java program, run from terminal
body: <p>At the moment I'm wrangling with an existing Java project, my goal is to create a command line interface for passing arguments to it, to "run" it, so to speak. </p>
  
  <p>I'm using the <a href="https://commons.apache.org/proper/commons-cli/" rel="nofollow noreferrer">Apache commons cli library</a>, like so: </p>
  
  <pre><code>import org.apache.commons.cli.CommandLine;
  import org.apache.commons.cli.CommandLineParser;
  import org.apache.commons.cli.DefaultParser;
  import org.apache.commons.cli.HelpFormatter;
  import org.apache.commons.cli.Options;
  import org.apache.commons.cli.ParseException;
  
  public class CommandLineParameters {
  
    public static void main(String[] args) {
      CommandLineParser parser = new DefaultParser();
      Options options = new Options();
  
      options.addOption("a", "abc", true, "First Parameter");
      options.addOption("x", "xyz", true, "Second Parameter");
      options.addOption("h", "help", false, "Shows this Help");
  
      try {
        CommandLine commandLine = parser.parse(options, args);
  
        System.out.println(commandLine.getOptionValue("a"));
        System.out.println(commandLine.getOptionValue("x"));
  
        if (commandLine.hasOption("h")) {
          HelpFormatter formatter = new HelpFormatter();
          formatter.printHelp("CommandLineParameters", options);
        }
  
      } catch (ParseException e) {
        e.printStackTrace();
      }
    }
  }
  </code></pre>
  
  <p>In Eclipse I can run this by modifying the <code>Arguments</code> passed to the <code>Run Configurations...</code>, however, what I would <em>in fact</em> like to do is, to be able to run this programme from a terminal shell, in an analogous way to "<em>normal</em>" terminal programmes such as <code>grep</code>, or <code>ipconfig</code>. </p>
  
  <p>How can this be achieved using Java?</p>
  
  <p><strong>NOTE:</strong>
  I'm not trying to <em>compile/build</em> this programme from the command line, I'm trying to make an <em>interface</em> to the program in the command line.</p>