title: Running Java program via Command Prompt with input and output files
body: <p>I have a program that is supposed to take inputs from the commandline and direct them to <strong>standard input</strong> and direct the output to <strong>standard output</strong>. The code supposed to be entered into the commandline is meant to look as follows:</p>
  
  <pre><code>java    package.sub.Calc    &lt;   input-file &gt;    output-file
  </code></pre>
  
  <p>or</p>
  
  <pre><code>echo    some    inputs  to  the calculator  |   java    
  package.sub.Calc
  </code></pre>
  
  <p>But I can't seem to get it to work correctly. The goal is to be able to pass a text file into the program or write your math problems right there on the command line and pipe it in.</p>
  
  <p>The program runs correctly for</p>
  
  <pre><code>java -cp . package.sub.Calc
  </code></pre>
  
  <p>and then having the user type in their problem and hitting enter.</p>
  
  <p>If  I have a file named <code>input.txt</code> and want to call it from the command line and have the answers print out in the commandline (program is designed to <code>System.out.println</code>) how would I be entering this information in?</p>
  
  <p>My current code implements a <code>Scanner</code> for <code>System.in</code>. Would I have to use anything else to get this to work? I am new to running anything in the command prompt and can't seem to get it to work.</p>
  
  <p>Any help is greatly appreciated.</p>
  
  <p>My code:</p>
  
  <pre><code>public static void main(String[] args) {
      Calc calc = new Calc();
      Scanner sc = new Scanner(System.in);
      StringBuilder sb = new StringBuilder();
      String input = "";
       List&lt;String&gt; strs = new ArrayList&lt;&gt;();
      ArrayList&lt;String&gt; tokens;
      while (sc.hasNextLine()) {
          String line = sc.nextLine().trim();
          if (line.isEmpty()) {
              for (String s : strs) {
                  sb.append(s);
                  input = sb.toString();
              }
              tokens = new ArrayList&lt;&gt;(Arrays.asList(input.split(" ")));
              //call calculate and reset
              calc.calculate(tokens);
              strs.clear();
              tokens.clear();
              sb.setLength(0);
              input = "";
          } else if (line.length() == 1) {
              strs.add(" ");
              strs.add(line);
              strs.add(" ");
          //manual user exit case
          } else if (line.equals("EXIT")){
              System.exit(0);
          }else {
              strs.add(line);
          }
      }
  }
  </code></pre>