1. Home
  2. command
  3. How To Pass Contents Of A Text File As Command Line Arguments For A Java Application?

How To Pass Contents Of A Text File As Command Line Arguments For A Java Application?

By WinPie Posted in: command

We were told to test a java project of ours by
doing entering this into the command prompt… java SLSim < sample-input.txt
This is supposed to read the contents of the text file line by line as if each were command line arguments. However, whenever I type exactly that I keep getting an ArrayOutofBounds exception or something like that. Normal arguments work like.. "java SLSim lineup" so I don't think it's so much an issue with my code…I'm probably missing something..but what?
Thanks in advance!

Get Chitika Premium
  1. om Says

    When you run a program by using a command line like this:
      java SLSim < sample-input.txt
    the ‘<‘ character means “take the content of the following file and feed that content into the program’s standard input stream”. The file content is not presented as arguments.
    Your program can obtain the file contents by reading them from the System.in stream.
    Depending on how you need to process the file’s contents, you might find it convenient to use a java.util.Scanner object on top of System.in rather than reading System.in directly.
    BTW, the fact that your program takes an ArrayOutOfBounds exception might mean that it always assumes that it will be given at least one command-line argument. If that’s what’s happening then that’s a bug in your program.

Leave a Reply

You must be logged in to post a comment.

More Interesting Things

©2011 Windows Pie, All rights reserved.