title: Java: Passing file name and string to the command line
body: <p>I have a small Java program where I'm trying to pass a text file and one or more strings separated by white spaces at the command line. Inside the text file, I have a list of packages as follows:</p>
  
  <pre><code>gui -&gt; awtui swingui swingui -&gt; runner extensions textui -&gt; runner
  framework awtui -&gt; runner runner -&gt; framework extensions -&gt; framework
  </code></pre>
  
  <p>Now in my java, I want to list each package and its dependencies by passing the text file and package ame at the commandline as follows:</p>
  
  <pre><code>$ java PackageList sample.txt gui swingui
  </code></pre>
  
  <p>When I press enter, I would like to list each package as follows at the console:</p>
  
  <pre><code>gui -&gt; awtui swingui
  swingui -&gt; runner extensions
  </code></pre>
  
  <p>As you can see, the rest of packages should not appear in the output if they are not passed at the command line as arguments. </p>
  
  <p>Here's my Java code which wish is currently printing all the contents of the file regardless of the command line arguments after file name:</p>
  
  <pre><code>public class PackageList {
  
      public static void main(String[] args) {
  
          File inputFile = null;
          if (args.length &gt; 0) {
              inputFile = new File(args[0]);
          } else {
              System.err.println("Invalid arguments count:" + args.length);
              System.exit(1);
          }
  
          BufferedReader br = null;
  
          try {
              String sCurrentLine;
              br = new BufferedReader(new FileReader(inputFile));
              while ((sCurrentLine = br.readLine()) != null) {
                  System.out.println(sCurrentLine);
              }
  
          } catch (IOException e) {
              e.printStackTrace();
          } finally {
              try {
                  if (br != null)
                      br.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
  
      }
  }
  </code></pre>